Notes on spring global exception handling

Use @ControllerAdvice + @ExceptionHandler for global Controller layer exception handling. As long as it is designed properly, there is no need to try-catch on the Controller layer anymore! Further, @ Validated abnormality check annotations may be processed together, without having to manually check result is determined to bind BindingResult / Errors of
a, disadvantages
advantages: data checking and abnormal abnormality Controller unitary layer, reducing Template code, reduce the amount of coding, improve scalability and maintainability.
Disadvantages: Exception Processing Controller can not capture layer (cast out) and for abnormal Interceptor (interceptor) layer, layer abnormal Spring framework, can not do anything
two basic uses examples
@ControllerAdvice annotation defines global exception handler class
@ControllerAdvice
public class GlobalExceptionHandler {
}
Please ensure that this GlobalExceptionHandler class can be scanned and loaded into the Spring container.
@ExceptionHandler annotation declares the exception handling method
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)
    @ResponseBody
    String handleException(){
        return "Exception Deal!";
    }
}The
handleException() method will handle all Exception thrown by the Controller layer and its subclasses. This is the most basic usage.
Parameter list annotated @ExceptionHandler method in
@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
@Documented
public @interface ExceptionHandler {


    / **
      * at The annotated by the Handled Exceptions and Method,. The If empty, by Will default to the any
      * exceptions listed in the method argument list.
      */
    Class<? extends Throwable>[] value() default {};


}
If the exception type to be handled is not declared in the @ExceptionHandler annotation, it will default to the exception type in the parameter list
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler ()
    @ResponseBody
    String handleException (Exception E) {
        return "Exception Deal!" + E.getMessage ();
    }
}
parameter object exception object is thrown Controller layer
three processing on the polishing layer service Service abnormality
package Business exception class:
public class BusinessException extends RuntimeException {


    public BusinessException(String message){
        super(message);
    }
} Service implementation class @Service public class DogService { @Transactional public Dog update(Dog dog){ // some database options // Simulated dog’s new name conflicts with other dog’s names BSUtil.isTrue(false, "The dog’s name has been used..."); // update database dog info





   
   


       


       
       


       


        return dog;
    }


}
The auxiliary tool class BSUtil
public static void isTrue(boolean expression, String error){
    if(!expression) {
        throw new BusinessException(error);
    }
}
Declare the business exception class in the GlobalExceptionHandler class, and make corresponding The processing is then returned to the user. Code closer to the real project
/**
 * Created by kinginblue on 2017/4/10.
 * @ControllerAdvice + @ExceptionHandler implements global Controller layer exception handling
 */
@ControllerAdvice
public class GlobalExceptionHandler {


    private static final Logger LOGGER = LoggerFactory. getLogger(GlobalExceptionHandler.class);


    /**
     * Handle all unknowable exceptions
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    AppResponse handleException(Exception e){
        LOGGER.error(e.getMessage(), e);


        AppResponse response = new AppResponse();
        response.setFail("操作失败!");
        return response;
    }


    /**
     * 处理所有业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    AppResponse handleBusinessException(BusinessException e){
        LOGGER.error(e.getMessage(), e);


        AppResponse response = new AppResponse();
} Note: The usage of @NotNull, @Min, and @NotBlank is beyond the scope of this article. If you are not familiar with it, please find the information to learn. other instructions:

























The @Data annotation is the annotation of the **Lombok** project, which can save us from manually adding getter & setter to the code.
When using in Eclipse and IntelliJ IDEA, you also need to install related plug-ins. This step is to Google/Baidu by yourself! For the Json interface of RESTFUL in SpringMVC, the data binding and verification are as follows: /**  * Example of using GlobalExceptionHandler to handle controller layer exceptions globally  * @param dog  * @return  */ @PatchMapping(value = "" ) AppResponse update(@Validated(Update.class) @RequestBody Dog dog){     AppResponse resp = new AppResponse();     // Perform business     Dog newDog = dogService.update(dog);     // Return data     resp.setData(newDog);     return resp; } Use @Validated + @RequestBody annotation to achieve.






















When the @Validated + @RequestBody annotation is used but the Errors type parameter declaration is not followed by the bound data object, the Spring MVC framework will throw a MethodArgumentNotValidException.


Therefore, by adding the declaration and handling of the MethodArgumentNotValidException exception in the GlobalExceptionHandler, the data validation exception can be processed globally
/**
 * Created by kinginblue on 2017/4/10.
 * @ControllerAdvice + @ExceptionHandler realizes the global Controller layer Exception handling
 */
@ControllerAdvice
public class GlobalExceptionHandler {


    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * Handle all unknowable exceptions
     * @param e
     * @return
     */
    @ExceptionHandler(Exception. class)
    @ResponseBody
    AppResponse handleException(Exception e){
        LOGGER.error(e.getMessage(), e);


        AppResponse response = new AppResponse();
        response.setFail("操作失败!");
        return response;
    }


    /**
     * 处理所有业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    AppResponse handleBusinessException(BusinessException e){
        LOGGER.error(e.getMessage(), e);


        AppResponse response = new AppResponse();
        response.setFail(e.getMessage());
        return response;
    }


    /**
     * Handle all interface data validation exceptions
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    AppResponse handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
        LOGGER.error(e.getMessage(), e);


        AppResponse response = new AppResponse();
        response.setFail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return response;
    }
}


Understand here: define the global exception handler through the controllerAdvice and ExceptionHandler annotations Intercepting all exceptions at the controller layer can distinguish the intercepted exception types and deal with them accordingly
   For example, error log printing and friendly response to exceptions (here you can customize the exception class and need to inherit RuntimeException to customize the attributes of the exception, such as adding error code and error information fields and exception information can be inherited from RuntimeException) The
   response here can be subpackaged The fields of the Respose object are respectively (success boolean type; errorCode, errorMsg, and data Object type)

Guess you like

Origin blog.csdn.net/u011445756/article/details/80174683