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