zoukankan      html  css  js  c++  java
  • springboot统一异常处理类及注解参数为数组的写法

    统一异常处理类

    package com.wdcloud.categoryserver.common.exception;
    
    import com.wdcloud.categoryserver.common.constant.CodeConstants;
    import com.wdcloud.categoryserver.common.entity.BaseView;
    import org.springframework.http.converter.HttpMessageNotReadableException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.support.MissingServletRequestPartException;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * @describe: 全局异常处理
     * @author: zhuchunwang
     * @date: 2018/5/29 17:40
     * @version: 1.0
     */
    @ControllerAdvice(annotations = {RestController.class})
    public class GlobalExceptionHandler {
        /**
         * 默认未知异常
         * @param req
         * @param e
         * @return
         * @throws Exception
         */
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public BaseView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
            e.printStackTrace();
            return  new BaseView(CodeConstants.SYSTEM_EXCEPTION,CodeConstants.SYSTEM_EXCEPTION_MSG);
        }
    
        /**
         * 参数异常
         * @param req
         * @param e
         * @return
         * @throws Exception
         */
        @ExceptionHandler(value = {HttpMessageNotReadableException.class, MissingServletRequestPartException.class})
        @ResponseBody
        public BaseView httpMessageNotReadableExceptionErrorHandler(HttpServletRequest req, Exception e) throws Exception {
            e.printStackTrace();
            return  new BaseView(CodeConstants.PARAMETER_ERROR,CodeConstants.PARAMETER_ERROR_MSG);
        }
    }

    一个参数时这样写

    @ExceptionHandler(value = HttpMessageNotReadableException.class)

    多个参数时这样写

    @ExceptionHandler(value = {HttpMessageNotReadableException.class, MissingServletRequestPartException.class})
  • 相关阅读:
    hihocoder1062 最近公共祖先·一
    POJ2342 Anniversary party(动态规划)(树形DP)
    【动态规划】抄近路(水题)
    【动态规划】数的划分 (动态规划)
    【动态规划】矩形嵌套 (DGA上的动态规划)
    hihocoder Popular Products(STL)
    hihocoder Counting Islands II(并查集)
    51nod 编辑距离问题(动态规划)
    51nod 最长公共子序列问题(动态规划)(LCS)(递归)
    目标提取——背景均匀、目标与背景相似
  • 原文地址:https://www.cnblogs.com/zhucww/p/9167857.html
Copyright © 2011-2022 走看看