Home > Net >  in kotlin data class, @field:NotBlank not working
in kotlin data class, @field:NotBlank not working

Time:11-03

We are currently using jdk 14, spring boot 2.2.8.RELEASE, and Kotlin 1.5.31. the below class used to error on blank first or name.

now that we are trying to update to spring boot 2.5.4 it's no longer throwing the error in our automated test case.

data class ChangeInformationRequest(
    @field:NotBlank var firstName: String,
    @field:NotBlank var lastName: String,
    @field:Valid var address: Address?
)

CodePudding user response:

The issue is that the Spring Boot validation starter is no longer included in web starters (which I am guessing you are using) from Spring Boot 2.3 (https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters). This means that spring-boot-starter-validation is no longer a transitive dependency of spring-boot-starter-web or spring-boot-starter-webflux. As a consequence you have to actually add the dependency yourself to your pom.xml as follows:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

I suggest you go through all the relevant Spring Boot release notes so that you can learn upfront which are the major changes that might affect your code:

  • Related