自定义全局异常

2018-03-01 11:18:33来源:oschina作者:haoyuehong人点击

分享

1、当接口返回json数据时,系统中应该有返回格式和字段,首先定义json返回时的统一字段,


① 建议idea中使用lombok的@Getter与@Setter注解,使用该注解后不必写get/set方法,会自动生成,避免修改字段后频繁修改方法降低开法效率


②有时返回的json数据中并不会包含数据,或者data为NULL,该字段非必要,此时,我们可以使用类注解


@JsonInclude(Include.NON_NULL)

来排除NULL字段,使其返回数据中不包含NULL字段


package com.hz1202.common;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@Getter
@JsonInclude(Include.NON_NULL)
public class JsonData implements Serializable {
private int status;
private String msg;
private T data;
private JsonData(int status) {
this.status = status;
}
private JsonData(String msg) {
this.msg = msg;
}
private JsonData(T data) {
this.data = data;
}
private JsonData(int status, String msg) {
this.status = status;
this.msg = msg;
}
private JsonData(int status, T data) {
this.status = status;
this.data = data;
}
private JsonData(int status, String msg, T data) {
this.status = status;
this.msg = msg;
this.data = data;
}
//使之不在json返回结果当中
@JsonIgnore
public boolean isSuccess() {
return this.status == 1;
}
public static JsonData createSuccess() {
return new JsonData(1);
}
public static JsonData createSuccess(String msg) {
return new JsonData(1, msg);
}
public static JsonData createSuccess(T data) {
return new JsonData(1, data);
}
public static JsonData createSuccess(String msg, T data) {
return new JsonData(1, msg, data);
}
public static JsonData createError() {
return new JsonData(1);
}
public static JsonData createError(String msg) {
return new JsonData(0, msg);
}
public static JsonData createError(int status, String msg) {
return new JsonData(status, msg);
}
public Map toMap() {
HashMap result = new HashMap();
result.put("status", status);
result.put("msg", msg);
if(data != null){
result.put("data", data);
}
return result;
}
}

2、定义我们自己的自定义异常,自定义异常需要继承RunTimeException,并覆写以下方法即可,idea覆写方法快捷键为“ctrl+o”


package com.hz1202.exception;
public class PermissionException extends RuntimeException{
public PermissionException() {
super();
}
public PermissionException(String message) {
super(message);
}
public PermissionException(String message, Throwable cause) {
super(message, cause);
}
public PermissionException(Throwable cause) {
super(cause);
}
protected PermissionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

3、自定义全局异常类


①、实现HandlerExceptionResolver接口


②代码中“jsonView”是spring-mvc配置文件spring-mvc.xml中配置处理json的bean的id


package com.hz1202.common;
import com.hz1202.exception.PermissionException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver{
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
String url = request.getRequestURL().toString();
ModelAndView mv;
String defaultMsg = "System error";
//定义文本请求和页面请求
//json请求
if(url.endsWith(".json")){
//判断是否是我们自定义的异常
if(e instanceof PermissionException){
//创建异常返回信息
JsonData jsonData = JsonData.createError(e.getMessage());
//封装异常信息
mv = new ModelAndView("jsonView",jsonData.toMap());
}else{
log.error("unkwon json exception={},url={},",e,url);
JsonData jsonData = JsonData.createError(defaultMsg);
mv = new ModelAndView("jsonView",jsonData.toMap());
}
}else if(url.endsWith(".page")){
//页面请求
log.error("unkwon page exception={},url={},",e,url);
JsonData jsonData = JsonData.createError(defaultMsg);
mv = new ModelAndView("exception",jsonData.toMap());
}else{
log.error("unkwon exception={},url={},",e,url);
JsonData jsonData = JsonData.createError(defaultMsg);
mv = new ModelAndView("jsonView",jsonData.toMap());
}
return mv;
}
}


3、在spring-mvc.xml中配置全局异常处理



最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台