I wan to return a list of errors using this Response object:
public class StringResponseDTO {
private String response;
public StringResponseDTO(String response) {
super();
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
I use this code to generate errors:
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println ("Validation error in field: " + error.getObjectName()
+ "! Validation error message: " + error.getDefaultMessage()
+ "! Rejected value:" + error.getRejectedValue());
return ResponseEntity.ok(new StringResponseDTO(error.getField() + " " + error.getDefaultMessage()));
}
I want to return a list like this:
response: {
errors: [
field_name: message,
second_name: second_message
]
}
Do you know how I can modify the code? Probably I need add constructor?
You need to use the following classes to model the above json:
You can construct the response as: