これは、プロジェクト内に通常、私たちは大漁の例外でラップしますが、そうすることがインパクトの生産に探します。エンコードするために多くのプロジェクトで標準化され、この記事の意志の詳細例外ベースのAOPのインターセプトを実装する方法
の結果を処理します:
404リクエスト例外
要求パラメータ・エラー
まず、オープニングAspectJのプロキシ設定は、このような容器は、AOPの設定SpringBootを解決することができます
@EnableAspectJAutoProxy(proxyTargetClass =真)
第二に、共通の戻りロジックを定義します
2.1一般的には、クラスCommonRes.classを返します。
package com.yfy.dianping.common;
/**
* @author youfy
*/
public class CommonRes {
/**
*请求的返回处理结果,"success"或"fail"
*/
private String status;
/**
* 若status=success时,表明对应的返回的json类数据
* 若status=fail时,则data内将使用通用的错误码对应的格式
*/
private Object data;
/**
*定义一个通用的创建返回对象的方法
*/
public static CommonRes create(Object result){
return CommonRes.create(result,"success");
}
public static CommonRes create(Object result,String status){
CommonRes commonRes = new CommonRes();
commonRes.setStatus(status);
commonRes.setData(result);
return commonRes;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
2.2一般的な例外クラスCommonError.class
package com.yfy.dianping.common;
/**
* @author youfy
*/
public class CommonError {
/**
* 错误码
*/
private Integer errCode;
/**
* 错误描述
*/
private String errMsg;
public CommonError(Integer errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
public CommonError(EmBusinessError emBusinessError){
this.errCode = emBusinessError.getErrCode();
this.errMsg = emBusinessError.getErrMsg();
}
public Integer getErrCode() {
return errCode;
}
public void setErrCode(Integer errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
2.3ビジネス異常BusinessException.class
package com.yfy.dianping.common;
/**
* @author youfy
*/
public class BusinessException extends Exception {
private CommonError commonError;
public BusinessException(EmBusinessError emBusinessError){
super();
this.commonError = new CommonError(emBusinessError);
}
public CommonError getCommonError() {
return commonError;
}
public void setCommonError(CommonError commonError) {
this.commonError = commonError;
}
}
2.4異常列挙EmBusinessError.class
package com.yfy.dianping.common;
/**
* @author youfy
*/
public enum EmBusinessError {
//通用的错误类型以10000开头
NO_OBJECT_FOUND(10001, "请求对象不存在"),
UNKNOWN_ERROR(10002, "未知异常"),
NO_HANDLE_FOUND(10003, "找不到执行的路径操作"),
BIND_EXCEPTION_ERROR(10004, "请求参数错误"),
;
private Integer errCode;
private String errMsg;
EmBusinessError(Integer errCode, String errMsg) {
this.errCode = errCode;
this.errMsg = errMsg;
}
public Integer getErrCode() {
return errCode;
}
public void setErrCode(Integer errCode) {
this.errCode = errCode;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
第三には、コントローラカバーにすべて@ControllerAdviceを使用して、グローバルな例外ハンドラを定義します
グローバル例外ハンドラGlobalExceptionHandler.class
package com.yfy.dianping.common;
import org.springframework.web.bind.ServletRequestBindingException;
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.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author youfy
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
@ResponseBody
public CommonRes doError(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,
Exception ex) {
if (ex instanceof BusinessException) {
return CommonRes.create(((BusinessException) ex).getCommonError(), "fail");
} else if (ex instanceof NoHandlerFoundException) {
CommonError commonError = new CommonError(EmBusinessError.NO_HANDLE_FOUND);
return CommonRes.create(commonError, "fail");
} else if (ex instanceof ServletRequestBindingException) {
CommonError commonError = new CommonError(EmBusinessError.BIND_EXCEPTION_ERROR);
return CommonRes.create(commonError, "fail");
} else {
CommonError commonError = new CommonError(EmBusinessError.UNKNOWN_ERROR);
return CommonRes.create(commonError, "fail");
}
}
}
第四に、404個の要求は例外、一連のプロセス:ゴミのデフォルトは404を処理し、見つけることができませんでしたプロセッサが例外をスローした場合
application.properties構成:
spring.resources.add-mappings=true
spring.mvc.throw-exception-if-no-handler-found=true
第五に、例外処理要求パラメータは、ServletRequestBindingExceptionによって捕捉します
if (ex instanceof NoHandlerFoundException) {
CommonError commonError = new CommonError(EmBusinessError.NO_HANDLE_FOUND);
return CommonRes.create(commonError, "fail");
}