spring-MVC和springBoot全局异常处理

 

1 创建全局异常处理方法

package com.exception;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.ResponseCode;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理
 */
public class MyExceptionHandler implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception ex) {
        ModelAndView mv = new ModelAndView();
        FastJsonJsonView view = new FastJsonJsonView();
        Map<String, Object> attributes = new HashMap<String, Object>();
        if (ex instanceof BizException) {
            ResponseCode responseCode = ((BizException) ex).getResponseCode();
            String exMessage = ex.getMessage();
            responseCode.setDesc(exMessage);
            attributes.put("success", responseCode.isSuccess());
            attributes.put("code", responseCode.getCode());
            attributes.put("desc", responseCode.getDesc());
        } else if (ex instanceof NumberFormatException) {
            ResponseCode responseCode = ResponseCode.OTHER_ERROR;
            responseCode.setDesc("数据类型有误");
            attributes.put("success", responseCode.isSuccess());
            attributes.put("code", responseCode.getCode());
            attributes.put("desc", responseCode.getDesc());
        } else {
            ex.printStackTrace();
            ResponseCode responseCode = ResponseCode.OTHER_ERROR;
            attributes.put("success", responseCode.isSuccess());
            attributes.put("code", responseCode.getCode());
            attributes.put("desc", responseCode.getDesc());
        }

        view.setAttributesMap(attributes);
        mv.setView(view);
        return mv;
    }
}

 

2创建自定义异常

package com.exception;

import com.response.ResponseCode;

/**
 * 业务异常
 */
public class BizException extends RuntimeException {

    private static final long serialVersionUID = -4319552887844403472L;


    private ResponseCode responseCode;



    public BizException(ResponseCode responseCode, Object... args) {
        super(String.format(responseCode.getDesc(), args));
        this.responseCode = responseCode;
    }

    public BizException() {
        super();
    }

    public BizException(String message, Throwable cause) {
        super(message, cause);
    }

    public BizException(Throwable cause) {
        super(cause);
    }

    public BizException(String message) {
        super(message);
    }

    public ResponseCode getResponseCode() {
        return responseCode;
    }

    /**
     * 实例化异常
     *
     * @param msgFormat
     * @param args
     * @return
     */
    public BizException newInstance(String msgFormat, Object... args) {
        return new BizException(null, msgFormat, args);
    }

}

3注册全局异常处理

注册方式

3.1.spring-mvc.xml中配置

<bean id="exceptionResolver" class="com.zyark.only.exception.MyExceptionHandler"/>

 3.2.config配置(springBoot可直接配置config生效)

import com.zyark.only.exception.MyExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author zzr
 */
@Configuration
public class DefaultShiroConfiguration {


    /**
     * 注册全局异常处理
     *
     * @return
     */
    @Bean(name = "exceptionHandler")
    public HandlerExceptionResolver handlerExceptionResolver() {
        return new MyExceptionHandler();
    }

}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值