如何在RESTful Spring MVC控制器中处理验证错误和异常?
问题内容:
例如,如何在此控制器操作方法中处理验证错误和可能的异常:
@RequestMapping(method = POST)
@ResponseBody
public FooDto create(@Valid FooDTO fooDto, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return null; // what to do here?
// how to let the client know something has gone wrong?
} else {
fooDao.insertFoo(fooDto); // What to do if an exception gets thrown here?
// What to send back to the client?
return fooDto;
}
}
问题答案: