SpringBoot 全局異常處理

      在項目運行過程當中,是否是各類異常,若是咱們每一個都須要用 try{}catch(Exception e){}  的話,那麼就會寫特別特別多的重複代碼,就會顯得很是low,因此Spring爲咱們提供了全局異常處理機制,咱們只須要往外拋異常就能夠了:java

1: 自定義異常類(沒有不影響,只演示一下)git

package com.gy.demo.common.handler;

/**
 * Description: 參數異常類
 *
 * @author geYang
 * @since 2017/12/28
 **/
public class ParamException extends RuntimeException {
	private int code;
	
	public ParamException(ResultEnum resultEnum){
		super(resultEnum.getMessage());
		this.code = resultEnum.getCode();
	}
	
	public int getCode () {
		return code;
	}
	
	public void setCode (int code) {
		this.code = code;
	}
}

2: 自定義返回狀態枚舉web

package com.gy.demo.common.handler;

/**
 * Description: 返回結果狀態碼
 *
 * @author geYang
 * @since 2017/12/28
 **/
public enum ResultEnum {
	PARAM_NULL(401,"參數爲空"),
	PARAM_ERROR(402,"參數異常"),
	
	SUCCESS(200,"SUCCESS"),
	RETURN_NULL(201,"返回值爲空"),
	;
	
	private int code;
	private String message;
	
	ResultEnum (int code, String message) {
		this.code = code;
		this.message = message;
	}
	
	public int getCode () {
		return this.code;
	}
	
	public String getMessage () {
		return this.message;
	}
	
	
}

3: 自定義返回狀態類:spring

package com.gy.demo.common.handler;

import org.springframework.http.HttpStatus;

import java.util.HashMap;

/**
 * Description: 請求返回狀態類
 *
 * @author geYang
 * @since 2017/12/18
 **/
public class R extends HashMap<String, Object> {
	
	public R() {
		put("code", HttpStatus.OK.value());
	}
	
	public R (ParamException paramException) {
		put("code", paramException.getCode());
		put("msg", paramException.getMessage());
	}
	
	public static R error () {
		return error( HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知異常,請聯繫管理員");
	}
	
	public static R error (String msg) {
		return error(HttpStatus.INTERNAL_SERVER_ERROR.value(),msg);
	}
	
	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}
	
	public static R ok(Object data) {
		R r = new R();
		r.put("data", data);
		return r;
	}

}

4: 異常處理類app

package com.gy.demo.common.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description: 全局異常處理類
 *
 * @author geYang
 * @since 2017/12/28
 **/
@ControllerAdvice
public class ExceptHandler {
	private Logger logger = LoggerFactory.getLogger(ExceptHandler.class);
	@ResponseBody
	@ExceptionHandler(value = Exception.class)
	public R doExceptHandler(Exception e){
		/* 判斷異常是否爲自定義異常 */
		if(e instanceof ParamException){
			ParamException p = (ParamException) e;
			return R.error(p.getCode(),p.getMessage());
		} else {
			logger.error("系統異常",e);
			return R.error(e.getMessage());
		}
	}


}

5: 測試:ide

@GetMapping("/test/{id}")
	public Object test(Integer id) throws Exception{
		if(id<5){
			throw new ParamException(ResultEnum.PARAM_ERROR);
		} else if (id<10) {
		
		}
		return R.ok(null);
	}

    參考大神: https://www.imooc.com/video/14341測試

    項目源碼: https://gitee.com/ge.yang/SpringBootthis

相關文章
相關標籤/搜索