例如,如何处理此 Controller 操作方法中的验证错误和可能的异常:
@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;
}
}
最佳答案
出错就抛异常,然后用@ExceptionHandler
to annotate another method然后它将处理异常并呈现适当的响应。
关于java - 如何处理 RESTful Spring MVC Controller 中的验证错误和异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9245487/