SpringBoot实现全局异常处理

在web开发的时候有时候希望系统处理业务逻辑是抛出的异常需要返回给客户。通过全局异常处理器可以简化服务器端至客户端的信息传递代码,我们只需要知道哪里抛出什么类型的异常,即可轻松将异常信息返回给前端。前端最好能定义一套对应的异常接受协议,在后期系统开发中能省不少时间。

1.自定义异常类

/**
 * 自定义异常类
 * @author lwh
 * 2019年2月28日 下午4:15:38
 */
public class FinanceException extends RuntimeException {

	private static final long serialVersionUID = 1L;
	private String exceptionCode;//异常状态码
	public FinanceException(String message,String exCode){
		super(message);
		this.exceptionCode = exCode;
	}
	public String getExceptionCode() {
		return exceptionCode;
	}
	public void setExceptionCode(String exceptionCode) {
		this.exceptionCode = exceptionCode;
	}
}

 2.返回数据处理类

/**
 * 处理返回数据的类
 * @author lwh
 * 2019年2月28日 下午4:15:28
 */
public class WebResult {
	    private String status; //状态码
	    private String msg; //提示信息
	    private String redirectUrl; //重定向的url
	    private boolean back; //返回
	    private boolean refresh; //刷新页面
	    private Map<String, Object> data; //数据
	    public WebResult(){
	        data = new HashMap<>();
	        refresh = false;
	        back = false;
	        status = "200";
	    }
	    public static WebResult buildResult(){
	        return new WebResult();
	    }
	    public WebResult status(String status){
	        setStatus(status);
	        return this;
	    }
	    public WebResult msg(String msg){
	        setMsg(msg);
	        return this;
	    }
	    public WebResult redirectUrl(String redirectUrl){
	        setRedirectUrl(redirectUrl);
	        return this;
	    }
	    public WebResult back(){
	        setBack(true);
	        return this;
	    }
	    public WebResult refresh(){
	        setRefresh(true);
	        return this;
	    }
	    public WebResult putData(String name, Object val){
	        data.put(name, val);
	        return this;
	    }

	  
	    public String getStatus() {
	        return status;
	    }
	    public void setStatus(String status) {
	        this.status = status;
	    }
	    public Map<String, Object> getData() {
	        return data;
	    }
	    public void setData(Map<String, Object> data) {
	        this.data = data;
	    }
	    public String getMsg() {
	        return msg;
	    }
	    public void setMsg(String msg) {
	        this.msg = msg;
	    }
	    public String getRedirectUrl() {
	        return redirectUrl;
	    }
	    public void setRedirectUrl(String redirectUrl) {
	        this.redirectUrl = redirectUrl;
	    }
	    public boolean isRefresh() {
	        return refresh;
	    }
	    public void setRefresh(boolean refresh) {
	        this.refresh = refresh;
	    }
	    public boolean isBack() {
	        return back;
	    }
	    public void setBack(boolean back) {
	        this.back = back;
	    }
	}

 3.异常处理类

@ControllerAdvice
public class ClobalExceptionHandler {
	@ExceptionHandler(FinanceException.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public Object financeExceptionHandler(FinanceException e) {
		e.printStackTrace();
		return WebResult.buildResult().status(e.getExceptionCode()).msg(e.getMessage());
	}

	// 其他未处理的异常
	@ExceptionHandler(Exception.class)
	@ResponseBody
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	public Object exceptionHandler(Exception e) {
		e.printStackTrace();
		return WebResult.buildResult().status("500").msg(e.getMessage());
	}
}

 测试类:

@RestController
public class HelloController {
	@RequestMapping("/hello")
	public String hello() {
		
		return "helloworld";
		
	}
	/**
	 * 异常测试
	 * lwh
	 * 2019年2月28日 下午4:15:46
	 * @return
	 */
	@RequestMapping("/ex")
	@ResponseBody
	public String ex(){
		throw new FinanceException("这里发生了一个业务异常", BusinessStatus.BUSINESS_ERR);
	}
	@RequestMapping("/exSys")
	@ResponseBody
	public String exSys() throws Exception{
		throw new Exception("系统异常");
	}
	

}

 测试结果:

url:http://localhost:8080/exSys 返回:{"status":"500","msg":"系统异常","redirectUrl":null,"back":false,"refresh":false,"data":{}}

url: http://localhost:8080/ex        返回:{"status":"600","msg":"这里发生了一个业务异常","redirectUrl":null,"back":false,"refresh":false,"data":{}}

 

posted on 2019-02-28 16:33  techno-geek  阅读(650)  评论(0)    收藏  举报