DTO:
public class User {
@NotNull
private String name;
@NotNull
private String password;
//..
}
Controller:
One way to do it is adding message in @NotNull annotation on entity properties. And adding @Valid annotation in controller request body.
DTO:
public class User {
@NotNull(message = "User name cannot be empty")
private String name;
@NotNull(message = "Password cannot be empty")
private String password;
//..
}
Controller:
@RequestMapping(value = "/user", method = RequestMethod.POST)
public ResponseEntity saveUser(@Valid @RequestBody User user) {
//..
return new ResponseEntity<>(HttpStatus.OK);
}
// Add one
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity> handleException(MethodArgumentNotValidException ex) {
// Loop through FieldErrors in ex.getBindingResult();
// return *YourErrorReponse* filled using *fieldErrors*
}