Czar
01/22/2020, 7:20 AMclass PersonForm(
@get:NotNull @get:Size(max=64)
private val name: String,
@get:Min(0)
private val age: Int
)
Does this mean that we can finally use non-nullable properties in validated pojos and still get a nice validation error instead of kotlin's null passed into non-nullable property?Quy D X Nguyen
01/22/2020, 2:36 PMQuy D X Nguyen
01/22/2020, 2:36 PMQuy D X Nguyen
01/22/2020, 2:36 PMCzar
01/22/2020, 4:55 PMBen Madore
01/22/2020, 7:18 PMleodeng
01/22/2020, 7:30 PMQuy D X Nguyen
01/23/2020, 1:11 AMCzar
01/23/2020, 11:43 AMdata class UglyButCourteousPerson(@field:NotNull @param:JsonProperty("name") private val _name: String?) {
val name @JsonIgnore get() = _name!!
}
data class NiceButBadBehavedPerson(@field:NotNull val name: String)
@RestController
class TestController {
@PostMapping("nice")
fun printPerson(@Valid @RequestBody person: UglyButCourteousPerson) {
println(person.toString())
}
@PostMapping("not-nice")
fun printPerson(@Valid @RequestBody person: NiceButBadBehavedPerson) {
println(person.toString())
}
}
testing:
POST localhost:8080/nice
{}
###
POST localhost:8080/not-nice
{}
/nice
will return nice validation error json and log at warn level
Field error in object 'uglyButCourteousPerson' on field '_name': rejected value [null]
/not-nice
will throw ugly (in this context) IllegalArgumentException
, pollute log with stack trace and return 500 http status to client.
I'm still looking for a way to avoid both IllegalArgumentException and _field
+ getter!!
ugliness
Is there a way to check the constraints before trying to create the pojo? At least for simple single-property constraints.
If there isn't are there any plans to improve this?Ben Madore
01/24/2020, 5:09 PMCzar
01/24/2020, 8:36 PM@ConstructorBinding @ConfigurationProperties
)Ben Madore
01/27/2020, 5:42 PMvalidator.validate(annotatedObject)
afaik it doesn’t have anything like validator.validate(source, potentialTarget)
to determine if the properties on source satisfy the annotations on target