[英]Handling exception on Spring MVC Controller
could you guys share some advices on how to design a good implementation for handling exception in Spring MVC? 你们可以分享一些关于如何设计一个好的实现来处理Spring MVC异常的建议吗? Here are some of my thoughts after spending some time online trying to figure out the suitable or better implementation in handling exception.
以下是我在网上花了一些时间试图找出处理异常的合适或更好的实现之后的一些想法。
Here is some background on the project I m working on: 以下是我正在进行的项目的一些背景知识:
So, here is my confusion. 所以,这是我的困惑。 There are two types of exceptions handling mechanism that I have thought of.
我想到了两种类型的异常处理机制。 Both were implemented using @ControllerAdvice, which separates the exception handling in a separate Java class.
两者都是使用@ControllerAdvice实现的,它将单独的Java类中的异常处理分开。
Approach A 方法A.
A same exception is reused for a particular method in a mvc controller. 对mvc控制器中的特定方法重用相同的异常。 For example,
例如,
public String methodA() throws AException{ // bla bla bla } public String methodA()抛出AException {// bla bla bla}
Then there will be unique AExceptionHandler handling this exception in a separate java file. 然后会有一个独特的AExceptionHandler在一个单独的java文件中处理这个异常。 There could be other method call in methodA() that throws different Exception.
在methodA()中可能有其他方法调用抛出不同的异常。 But, a try catch block would be implemented to wrap the method throwing other type of Exception and re-throw with AException.
但是,将实现一个try catch块来包装该方法抛出其他类型的Exception并使用AException重新抛出。
Approach B 方法B.
So, these are issues I m facing now. 所以,这些是我现在面临的问题。 I hope that I'm moving in the correct direction in terms of handling exception for Spring web.
我希望我在处理Spring Web异常方面朝着正确的方向前进。 Please let me know you thoughts on this.
请让我知道你的想法。 Thanks.
谢谢。
Example: 例:
Lets use the account signup method as example: 让我们使用帐户注册方法作为示例:
//In the controller class
public String accountSignUp(@Valid AccountSignUpForm form){
if(bindingResult.hasErrors()){
return "signupview";
}
accountSignUpSvc.validateEmail(form.getEmail);
accountSignUpSvc.createAccount(form);
}
// In the accountSignUpSvc class;
public interface AccountSignUpSvc {
// Check if email has been used for sign up.
void validateEmail(String email) throws DuplicateAccountException;
// Create the account based on the form.
void createAccount(AccountSignUpForm form) throws AccountCreationException;
}
So using approach A: 所以使用方法A:
//In the controller class
public String accountSignUp(@Valid AccountSignUpForm form){
if(bindingResult.hasErrors()){
return "signupview";
}
try{
accountSignUpSvc.validateEmail(form.getEmail);
} catch ( DuplicateAccountException e){
// Rethrow with a controller method specific exception
throw new AccountSignUpException("Returned error message"," Internal error message.",e);
}
// Then implement the similar try catch block for the accountSignUpSvc.createAccount method
}
@ControllerAdvice
public class AccountSignUpExceptionHandler{
@ExceptionHandler(AccountSignUpException.class)
public ModelAndView handleAccountSignUpException(HttpServletRequest request, AccountSignUpException ex){
log.error(ex.getInternalError);
ModelAndView mav = new ModelAndView("signup");
mav.addObject("returnError", ex.getReturnError());
return mav;
}
Then using approach B: 然后使用方法B:
//In the controller class
public String accountSignUp(@Valid AccountSignUpForm form){
if(bindingResult.hasErrors()){
return "signupview";
}
accountSignUpSvc.validateEmail(form.getEmail);
// Then implement the similar try catch block for the accountSignUpSvc.createAccount method
}
@ControllerAdvice
public class AccountSignUpExceptionHandler{
@ExceptionHandler(DuplicateAccountException.class)
public ModelAndView handleDuplicateAccountException(HttpServletRequest request, DuplicateAccountException ex){
log.error("Error due to error");
ModelAndView mav = new ModelAndView("signup");
mav.addObject("returnError", "Return error message: a long error message.");
return mav;
}
The benefit I mentioned is that the exception handler can be specific as only a certain condition would trigger it. 我提到的好处是异常处理程序可以是特定的,因为只有某个条件会触发它。 So, any long return message that is supposed to be displayed at the html page can be coded in this specific exception handler.
因此,应该在html页面上显示的任何长返回消息都可以在此特定异常处理程序中进行编码。
In my opinion it depends on whether these exceptions you are talking about are checked exceptions or not. 在我看来,这取决于你所讨论的这些例外是否是经过检查的例外情况。 If they are all checked exceptions being thrown in methodA() then I would be inclined to use Approach A and catch these exceptions and return them as your own app specific exception with a single handler in your @ControllerAdvice class.
如果它们都是在methodA()中抛出的所有检查异常,那么我倾向于使用方法A并捕获这些异常并将它们作为您自己的应用程序特定异常返回,并在@ControllerAdvice类中使用单个处理程序。
If however you are wanting to handle error responses for unchecked exceptions that you might expect to occur in your application then I would use approach B. 但是,如果您想要处理应用程序中可能出现的未经检查的异常的错误响应,那么我将使用方法B.
for exception unique field: 对于异常唯一字段:
try{
}
catch(DataIntegrityViolationException e)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.