前提・実現したいこと
KotlinとSpring Bootを用いてWebアプリケーション作成の学習をしています。
その際に、data classでtypemismatchが起きるとコントローラーにバインディングされるデータがnullになってしまうという状況が発生しています。
例:SignupFormのageにフォームで文字列が入力されPOSTされる場合
下記のコードのようにnullableにすることで現状は回避することが可能なのですが、他のバリデーションに引っかかった際にはnullが代入されることが無く、違いがわからずに困っております。Kotlinのnull安全を出来る限り守りたいため可能であればnullableにしたくありません。
kotlin
1@ModelAttribute @Validated form: SignupForm?
該当のソースコード
一部省略しております。
Kotlin
1data class SignupForm( 2 @field:NotBlank(message="{require_check}", groups = [ValidGroup1::class]) 3 var userName: String?, 4 5 @field:NotNull(message="{require_check}", groups = [ValidGroup1::class]) 6 @field:Min(value = 20, message = "{min_check}", groups = [ValidGroup2::class]) 7 @field:Max(value = 100, message = "{max_check}", groups = [ValidGroup2::class]) 8 var age: Int = 0, 9)
Kotlin
1@Controller 2class SignupController { 3 @GetMapping("/signup") 4 fun getSignUp(@ModelAttribute form: SignupForm?, model: Model): String { 5 return "login/signup" 6 } 7 8 @PostMapping("/signup") 9 fun postSignUp(@ModelAttribute @Validated form: SignupForm?, bindingResult: BindingResult, model: Model): String { 10 println(form.toString()) // ここでnullと表示される(typemismatchの場合) 11 if(bindingResult.hasErrors()) { 12 return getSignUp(form, model) 13 } 14 15 println(form.toString()) 16 17 return "redirect:/login" 18 } 19}
発生している問題・エラーメッセージ
SignupFormをnullableにしない場合に発生するエラーです。
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method com.example.SpringSample.login.controller.SignupController.postSignUp, parameter form at com.example.SpringSample.login.controller.SignupController.postSignUp(SignupController.kt) ~[main/:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.6.RELEASE.jar:5.2.6.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na] at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.35.jar:9.0.35] at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
補足情報(FW/ツールのバージョンなど)
Gradleの情報です。
gradle
1import org.jetbrains.kotlin.gradle.tasks.KotlinCompile 2 3plugins { 4 id("org.springframework.boot") version "2.3.0.RELEASE" 5 id("io.spring.dependency-management") version "1.0.9.RELEASE" 6 kotlin("jvm") version "1.3.72" 7 kotlin("plugin.spring") version "1.3.72" 8} 9 10group = "com.example" 11version = "0.0.1-SNAPSHOT" 12java.sourceCompatibility = JavaVersion.VERSION_11 13 14repositories { 15 mavenCentral() 16} 17 18dependencies { 19 implementation("org.springframework.boot:spring-boot-starter-jdbc") 20 implementation("org.springframework.boot:spring-boot-starter-thymeleaf") 21 implementation("org.springframework.boot:spring-boot-starter-web") 22 implementation("org.springframework.boot:spring-boot-starter-validation") 23 implementation("com.fasterxml.jackson.module:jackson-module-kotlin") 24 implementation("org.jetbrains.kotlin:kotlin-reflect") 25 implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") 26 implementation("org.webjars:jquery:3.5.1") 27 implementation("org.webjars:bootstrap:4.5.0") 28 developmentOnly("org.springframework.boot:spring-boot-devtools") 29 runtimeOnly("com.h2database:h2") 30 testImplementation("org.springframework.boot:spring-boot-starter-test") { 31 exclude(group = "org.junit.vintage", module = "junit-vintage-engine") 32 } 33} 34 35tasks.withType<Test> { 36 useJUnitPlatform() 37} 38 39tasks.withType<KotlinCompile> { 40 kotlinOptions { 41 freeCompilerArgs = listOf("-Xjsr305=strict") 42 jvmTarget = "1.8" 43 } 44} 45
あなたの回答
tips
プレビュー