Can anyone tell me what I am missing in this code? Spring Boot 3.2.3 and this dep is in place:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
@RestController
@Validated
public class TestController {
@PostMapping("/test")
public ResponseEntity<?> test(@Valid @RequestBody Test test) {
System.out.println(test);
return ResponseEntity.ok().build();
}
}
@Valid
public class Test {
@Valid
@NotNull
private String text;
@Valid
@NotNull
public String getText() {
return text;
}
@Valid
@NotNull
public void setText(@Valid @NotNull final String text) {
this.text = text;
}
}
I tried a dozen variants with the annotations but it still doesn't work. POST'ing this payload returns 200 OK:
{
}
or this
{
"test": {
}
}
or this
{
"test": {
"text": null
}
}
Output on console: null
Rewrite Test class as below.
Then to customize the validation response can do as below.
Tested with these inputs:
Response