SpringMVC统一异常处理 返回JSON解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xin917480852/article/details/78023911

1.目的

本方案通过JSON的格式将后台出现的错误通过JSON传递给前台,主要用于Ajax发起请求而后台操作不通过时的解决方案

2.代码

统一异常 JSON返回对象
package bishe.exception;

/**
 * @author Xin
 *统一JSON返回对象
 * @param <T>
 */
public class ErrorInfo<T> {
	
	public static final Integer OK = 0;
	public static final Integer ERROR = -1;
	
	private Integer code;
	private String message;
	private String url;
	private T data;
	
	public ErrorInfo() {
		
	}
	
	public ErrorInfo(Integer code) {
		this.code = code;
		
	}
	
	public ErrorInfo(Integer code,String message) {
		this.code = code;
		this.message = message;	
	}
	
	public ErrorInfo(Integer code,String message,String url) {
		this.code = code;
		this.message = message;	
		this.url = url;
	}
	
	public ErrorInfo(Integer code,String message,String url,T data) {
		this.code = code;
		this.message = message;	
		this.url = url;
		this.data = data;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public static Integer getOk() {
		return OK;
	}

	public static Integer getError() {
		return ERROR;
	}

	@Override
	public String toString() {
		return "ErrorInfo [code=" + code + ", message=" + message + ", url=" + url + ", data=" + data + "]";
	}
	
	
}
自定义异常AjaxException
package bishe.exception;

public class AjaxException extends RuntimeException{
	
	 public AjaxException(String message) {
		 super(message);
	}	
	 public AjaxException() {
		 super();
	 }

}
全局统一异常处理
package bishe.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;
import org.apache.poi.ss.formula.functions.T;
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.servlet.config.annotation.EnableWebMvc;

import bishe.exception.AjaxException;

@ControllerAdvice
@EnableWebMvc
public class GlobalExceptionHandler{

	@ExceptionHandler(AjaxException.class)
	@ResponseBody
	public ErrorInfo<String>ajaxException(HttpServletRequest req,Exception e){
		ErrorInfo<String> errInfo = new ErrorInfo<String>();
		errInfo.setCode(ErrorInfo.ERROR);
		errInfo.setMessage(e.getMessage());
		errInfo.setUrl(req.getRequestURI().toString());
		errInfo.setData("some data");
		return errInfo;
	}


}
模拟抛出异常
	
				@RequestMapping(value = "/sx.do", method = { RequestMethod.GET, RequestMethod.POST })
				public ModelAndView sx(Integer type) {
					
				throw new AjaxException("Ajax调用出错");
				}	
	

访问commic/borrow/sx.do【根据自己的路径访问】
返回JSON异常

流程:
程序补抓到异常抛出,统一在control层抛出,如果是在DAO层、Service层抛出到control层,然后全局的自定义异常处理类会捕获,对异常进行处理,通过@responseBody注解返回JSON。
@responseBody 是一个注解,通过引入其他库的,具体自己查阅。
 




猜你喜欢

转载自blog.csdn.net/xin917480852/article/details/78023911
今日推荐