1. 程式人生 > >java捕獲全域性異常

java捕獲全域性異常

java捕獲全域性異常

  • @ControllerAdvice是一個@Component,用於定義@ExceptionHandler@InitBinder@ModelAttribute方法,適用於所有使用@RequestMapping方法。

  • Spring4之前,@ControllerAdvice在同一排程的Servlet中協助所有控制器。Spring4已經改變:@ControllerAdvice支援配置控制器的子集,而預設的行為仍然可以利用。

  • 在Spring4中, @ControllerAdvice通過annotations()basePackageClasses()

    basePackages()方法定製用於選擇控制器子集。

不過據經驗之談,只有配合@ExceptionHandler最有用,其它兩個不常用。

@ControllerAdvice用來處理異常

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;

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


@ControllerAdvice
public class ControllerAdvice {

	private static final Logger log = LoggerFactory.getLogger(RuleMappingAdapterImpl.class);
	/**
	 * 全域性異常捕捉處理
	 * 
	 * @param ex
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(value = Exception.class)
	public Map<String, Object> errorHandler(Exception ex) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", Constant.ExeceptionCode.E_500);
		map.put("msg", ex.getMessage());
		log.error("捕捉到全域性異常(Exception),msg:{}", map.get("msg"), ex);
		return map;
	}

	/**
	 * 攔截捕捉自定義異常 MyException.class
	 * 
	 * @param ex
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler(value = CException.class)
	public Map<String, Object> myErrorHandler(CException ex) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", ex.getCode());
		map.put("msg", ex.getMsg());
		if(ex.getContext() == null ){
			map.put("class", "");
		}else {
			map.put("class", ex.getContext().getClass().getName());

		}
		log.error("捕捉到全域性異常(CntenException),msg:{}", map.get("msg"), ex.getExe());
		return map;
	}
}

public class Constant {
	public static class ExeceptionCode {
		public static int E_500 = 500;// 系統異常
		public static int E_200 = 200;// 正常訪問
		public static int E_403 = 403;// 資源無權訪問
		public static int E_401 = 401;// 使用者驗證失敗
		/**
		 * 各自模組自己定義
		 */
	}
}

public class CException extends RuntimeException {
	private static final long serialVersionUID = -1433777525759711883L;

	public CException(Object context, String code, String msg) {
		this.code = code;
		this.msg = msg;
		this.context = context;
	}

	public CException(Object context, String code, String msg, Exception exe) {
		this.code = code;
		this.msg = msg;
		this.context = context;
		this.exe = exe;
	}

	private Object context;
	private String code;
	private String msg;
	private Exception exe;

	public Object getContext() {
		return context;
	}

	public void setContext(Object context) {
		this.context = context;
	}

	public String getCode() {
		return code;
	}

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

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Exception getExe() {
		return exe;
	}

	public void setExe(Exception exe) {
		this.exe = exe;
	}
}