您的位置:首页 > 编程语言 > Java开发

spring-MVC和springBoot全局异常处理

2019-06-30 21:57 731 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_31728205/article/details/94359275

spring-MVC

全局异常处理方法

 

[code]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;
}
}

 

异常方法

[code]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);
}

}

 

 

1.spring-mvc.xml中配置

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

 2.config配置 

[code]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();
}

}

 

 

 

springBoot

使用上面的config配置

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: