• SpringBoot项目统一处理异常


    package com.chitic.module.core.aop;
    
    import com.chitic.module.core.constant.ChiticCoreConstant;
    import com.chitic.module.core.enums.ChiticResponseCode;
    import com.chitic.module.core.exception.ChiticException;
    import com.chitic.module.core.response.ChiticResponse;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.validation.BindException;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.MethodArgumentNotValidException;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.RestControllerAdvice;
    
    import javax.servlet.http.HttpServletRequest;
    
    @RestControllerAdvice(ChiticCoreConstant.BASE_PACKAGE)
    @Slf4j
    public class RestControllerExceptionAdvice {
    
       //此处可以拦截JSR303校验不加BindingResult抛出的异常,自定义异常类,按照自己想要的 返回 @ExceptionHandler(BindException.
    class) public ChiticResponse bindExceptionHandler(BindException ex) { BindingResult bindingResult = ex.getBindingResult(); String errorMsg = ""; if (bindingResult != null && bindingResult.getFieldError() != null) { errorMsg = bindingResult.getFieldError().getDefaultMessage(); } return ChiticResponse.fail(ChiticResponseCode.PARAMS_VALUE_ERROR.getCode(), errorMsg); } @ExceptionHandler(MethodArgumentNotValidException.class) public ChiticResponse methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException ex) { BindingResult bindingResult = ex.getBindingResult(); String errorMsg = ""; if (bindingResult != null && bindingResult.getFieldError() != null) { errorMsg = bindingResult.getFieldError().getDefaultMessage(); } return ChiticResponse.fail(ChiticResponseCode.PARAMS_VALUE_ERROR.getCode(), errorMsg); } @ExceptionHandler(ChiticException.class) public ChiticResponse chiticExceptionHandler(HttpServletRequest request, ChiticException ex) { return ChiticResponse.fail(ex.getCode(), ex.getMessage()); } @ExceptionHandler(Exception.class) public ChiticResponse globalExceptionHandler(HttpServletRequest request, Exception ex) { ex.printStackTrace(); log.error(ex.getMessage()); return ChiticResponse.fail(ChiticResponseCode.SYSTEM_ERROR.getCode(), ChiticResponseCode.SYSTEM_ERROR.getMessage()); } @ExceptionHandler(Throwable.class) public ChiticResponse globalExceptionHandler(HttpServletRequest request, Throwable ex) { ex.printStackTrace(); log.error(ex.getMessage()); return ChiticResponse.fail(ChiticResponseCode.SYSTEM_ERROR.getCode(), ChiticResponseCode.SYSTEM_ERROR.getMessage()); } }

    自定义异常类

    package com.chitic.module.core.exception;
    
    import com.chitic.module.core.enums.ChiticResponseCode;
    import com.chitic.module.core.response.ChiticResponse;
    import lombok.*;
    
    /**
     * @author GX
     * @Description: 全局异常
     * @date 2019/5/11 10:18
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    @EqualsAndHashCode(callSuper = false)
    public class ChiticException extends RuntimeException {
    
        /**
         * 错误编号
         */
        private String code;
    
        /**
         * 错误信息
         */
        private String message;
    
        public static ChiticException of(ChiticResponse response) {
            return ChiticException.builder()
                    .code(response.getCode())
                    .message(response.getMessage())
                    .build();
        }
    
        public static ChiticException of(String code, String message) {
            return ChiticException.builder()
                    .code(code)
                    .message(message)
                    .build();
        }
    
        public static ChiticException of(ChiticResponseCode responseCode) {
            return ChiticException.builder()
                    .code(responseCode.getCode())
                    .message(responseCode.getMessage())
                    .build();
        }
    
    
    }
  • 相关阅读:
    【BZOJ2424】[HAOI2010]订货 最小费用流
    【BZOJ1935/4822】[Shoi2007]Tree 园丁的烦恼/[Cqoi2017]老C的任务 树状数组
    【BZOJ2500】幸福的道路 树形DP+RMQ+双指针法
    【BZOJ4726】[POI2017]Sabota? 树形DP
    【BZOJ4883】[Lydsy2017年5月月赛]棋盘上的守卫 KM算法
    【BZOJ4881】5月月赛D 线段游戏 树状数组+set
    【BZOJ4518】[Sdoi2016]征途 斜率优化
    【BZOJ4818】[Sdoi2017]序列计数 DP+矩阵乘法
    【BZOJ2553】[BeiJing2011]禁忌 AC自动机+期望DP+矩阵乘法
    【BZOJ3211】花神游历各国 并查集+树状数组
  • 原文地址:https://www.cnblogs.com/gaomanito/p/10863765.html
Copyright © 2020-2023  润新知