It’s entirely possible this is a me problem, but from what I can tell at the moment it doesn’t seem that validation annotations are working. I’m using springboot 2.6.7 with kotlin 1.6.21 and hibernate-validator 7.0.4. Some sample code that I’m using:
// Gradle
implementation("org.hibernate.validator:hibernate-validator:7.0.4.Final")
// Spring RestController
@PostMapping("/test")
fun test(@Valid @RequestBody data: Data, errors: Errors) : ResponseEntity<Any> {
return if (errors.hasErrors()) {
ResponseEntity(errors.allErrors, HttpStatus.BAD_REQUEST)
} else {
ResponseEntity(data, HttpStatus.ACCEPTED)
}
}
// Kotlin POJO
class Data(
@get:NotNull(message = "First name cannot be null")
var firstName: String?,
@get:Pattern(regexp = "Y|N", message = "Last name must be a Y or N")
var lastName: String
)
No errors are returned when the validation rules are broken. Sending in a last name of “test” or a null firstname has no issues. I’ve tried changing @get → @field too. However, if I implement a custom validator and then call DataValidator().validate(data, errors) on the data POJO the errors are then returned properly.
I’ve also downgraded hibernate validator to 6.0.2-FINAL and the validation works properly with only the annotations.
Am I missing something here?