I have an controller that returns JSON. It takes a form, which validates itself via spring annotations. I can get FieldError list from BindingResult, but they don’t contain the text that a JSP would display in the <form:errors> tag. How can I get the error text to send back in JSON?
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) {
if (result.hasErrors()) {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.ERROR);
//HOW DO I GET ERROR MESSAGES OUT OF BindingResult???
} else {
JSONResponse r = new JSONResponse();
r.setStatus(JSONResponseStatus.OK);
return r;
}
}
JSONREsponse class is just a POJO
public class JSONResponse implements Serializable {
private JSONResponseStatus status;
private String error;
private Map<String,String> errors;
private Map<String,Object> data;
// ...getters and setters...
}
Calling BindingResult.getAllErrors() returns an array of FieldError objects, but it does not have the actual errors.
Disclaimer: I still do not use Spring-MVC 3.0
But i think the same approach used by Spring 2.5 can fullfil your needs
I hope it can be useful to you
UPDATE
If you want to get the message provided by your resource bundle, you need a registered messageSource instance (It must be called messageSource)
Inject your MessageSource instance inside your View
And to get your message, do as follows
Your Validator should looks like