I am having this UserDto
class which I want to put under @Valid
@Getter @Setter
public class UserDto{
@NotEmpty(message = "Name cannot be empty")
private String name;
@NotNull(message = "Provide a valid age")
private Integer age;
}
I have some requirements where I can't put validation on controller method, but I need to do it on the service method.
@PostMapping(value = "/user")
public void create(@RequestBody UserDto user){
userService.create(user);
}
public void create(@Valid UserDto user){
System.out.println(user);
}
But this is not working. And if I put @Valid
on the controller method, I do get the validation errors as expected.
Why so? Does serialization/deserialization has something to do with valition?