SpringBoot 统一异常处理

统一异常处理:

@ControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(AuthException.class)
    @ResponseBody
    public R handleRRException(AuthException e){
        R r = new R();
        r.put("code", e.getCode());
        r.put("msg", e.getMessage());
        return r;
    }
    
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.error();
    }
}

现在网上一般都是这种比较简单的写法

还有其他方式:

public class ControllerExceptionResolver extends ExceptionHandlerExceptionResolver {

    private static final Logger LOG = LoggerFactory.getLogger(ControllerExceptionResolver.class);

    protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) {
        String uri = request.getRequestURI();
        LOG.error("异常url:" + uri + ",处理框架", exception);
        if (exception instanceof StoneSystemRuntimeException) {
            // TODO 攻击或被异常访问才会出现
            StoneSystemRuntimeException stoneRuntimeException = (StoneSystemRuntimeException) exception;
            BasicRes res = new BasicRes();
            res.setMsg(stoneRuntimeException.getMsg());
            res.setResCode(stoneRuntimeException.getCode());
            this.excuteJson(response, res);
            LOG.error("系统运行时异常", exception);
        } else if (exception instanceof StoneBizzRuntimeException) {
            // TODO 普通业务异常
            StoneBizzRuntimeException stoneBizzException = (StoneBizzRuntimeException) exception;
            BasicRes res = new BasicRes();
            res.setMsg(stoneBizzException.getMsg());
            res.setResCode(stoneBizzException.getCode());
            this.excuteJson(response, res);
            LOG.error("业务异常", exception);
        } else {
            // TODO 其他未处理异常
            BasicRes res = new BasicRes();
            res.setMsg("系统更新中,请稍后再试");
            res.setResCode(ErrorCode.SYSTEM_EXCEPTION.getCode());
            this.excuteJson(response, res);
            LOG.error("未定义异常", exception);
        }
        return new ModelAndView();

    }

    private String excuteJson(HttpServletResponse response, BasicRes res) {
        try {
            byte[] jsonBytes = JSON.toJSONBytes(res);
            String exJson = new String(jsonBytes, SysConstant.CHARSET_UTF8);
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(exJson);
            return exJson;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

}

替换默认:

@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter {

    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        super.extendHandlerExceptionResolvers(exceptionResolvers);
        exceptionResolvers.add(this.getControllerExceptionResolver());
    }

    @Bean
    public ControllerExceptionResolver getControllerExceptionResolver() {
        return new ControllerExceptionResolver();
    }
}

继承实现ExceptionHandlerExceptionResolver类,这个类一般多见在SpringMVC中,但是SpringBoot中也可以继续使用

http://blog.didispace.com/springbootexception/

http://www.cnblogs.com/xinzhao/p/4902295.html

https://juejin.im/entry/5a5f3d61f265da3e5537f113

猜你喜欢

转载自www.cnblogs.com/hongdada/p/9157130.html