SpringBoot the global exception handler

Reference: Here

UI unified format type return

. 1  public  class CommonReturnType {
 2      // returns the data to the UI 
. 3      Private Object Data;
 . 4      // returns the state to the UI 
. 5      Private String Status;
 . 6  
. 7      public  static CommonReturnType Create (Result Object) {
 . 8          // default return status is success 
. 9          return CommonReturnType.create (Result, "Success" );
 10      }
 . 11      public  static CommonReturnType Create (Result Object, String Status) {
 12 is          CommonReturnType type = new new CommonReturnType();
13         type.setData(result);
14         type.setStatus(status);
15         return type;
16     }
17 
18     public Object getData() {
19         return data;
20     }
21 
22     public void setData(Object data) {
23         this.data = data;
24     }
25 
26     public String getStatus() {
27         return status;
28     }
29 
30     public void setStatus(String status) {
31         this.status = status;
32     }
33 }

 

Custom exception format

1 public interface CommonError {
2     public int getErrCode();
3     public String getErrMsg();
4     public CommonError setErrMsg(String errMsg);
5 }

 

. 1  public  enum EmBusinessError the implements CommonError {
 2      // begin 10,000 generic type of error 
. 3      PARAMETER_VALIDATION_ERROR (10001, "invalid parameters" ),
 . 4  
. 5      // unknown error 
. 6      UNKNOW_ERROR (10002, "Unknown error" ),
 . 7  
. 8      // 20,000 beginning with the user information related to errors 
9      USER_NOT_EXIST (20001, "user does not exist" ),
 10      User_LOGIN_ERROR (20002, "phone number or password is incorrect" ),
 11      User_NOT_LOGIN (20003, "not user login" ),
 12  
13      // at the beginning 30,000 transaction information is abnormal 
14      STOCK_NOT_ENOUGH (30001, "Inventory shortage")
15     ;
16     private int errCode;
17     private String errMsg;
18 
19     EmBusinessError(int errCode, String errMsg) {
20         this.errCode = errCode;
21         this.errMsg = errMsg;
22     }
23 
24     @Override
25     public int getErrCode() {
26         return this.errCode;
27     }
28 
29     @Override
30     public String getErrMsg() {
31         return this.errMsg;
32     }
33 
34     @Override
35     public CommonError setErrMsg(String errMsg) {
36         this.errMsg = errMsg;
37         return this;
38     }
39 }

 

. 1  public  class BusinessException the extends Exception the implements CommonError {
 2      Private CommonError CommonError;
 . 3  
. 4      // directly received EmBussinessError configured to pass the parameters for the abnormal traffic 
. 5      public BusinessException (CommonError CommonError) {
 . 6          Super ();
 . 7          the this .commonError = CommonError;
 . 8      }
 . 9  
10      @ accept the custom business exception errMsg constructed 
. 11      public BusinessException (CommonError CommonError, String errMsg) {
 12 is          Super ();
 13 is         this.commonError=commonError;
14         this.commonError.setErrMsg(errMsg);
15     }
16 
17     @Override
18     public int getErrCode() {
19         return this.commonError.getErrCode();
20     }
21 
22     @Override
23     public String getErrMsg() {
24         return this.commonError.getErrMsg();
25     }
26 
27     @Override
28     public CommonError setErrMsg(String errMsg) {
29         return this.commonError.setErrMsg(errMsg);
30     }
31 }

 

Global exception handler

 1 import com.wu.error.BusinessException;
 2 import com.wu.error.EmBusinessError;
 3 import com.wu.response.CommonReturnType;
 4 import org.springframework.web.bind.ServletRequestBindingException;
 5 import org.springframework.web.bind.annotation.ControllerAdvice;
 6 import org.springframework.web.bind.annotation.ExceptionHandler;
 7 import org.springframework.web.bind.annotation.ResponseBody;
 8 import org.springframework.web.servlet.NoHandlerFoundException;
 9 
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.util.HashMap;
13 import java.util.Map;
14 
15 @ControllerAdvice
16 public class GlobalExceptionHandler{
17     @ExceptionHandler(Exception.class)
18     @ResponseBody
19     public CommonReturnType doError(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception ex) {
20         ex.printStackTrace();
21         Map<String,Object> responseData = new HashMap<>();
22         if( ex instanceof BusinessException){
23             BusinessException businessException = (BusinessException)ex;
24             responseData.put("errCode",businessException.getErrCode());
25             responseData.put("errMsg",businessException.getErrMsg());
26         }else if(ex instanceof ServletRequestBindingException){
27             responseData.put("errCode", EmBusinessError.UNKNOW_ERROR.getErrCode());
28             responseData.put("errMsg","url绑定路由问题");
29         }else{
30             responseData.put("errCode", EmBusinessError.UNKNOW_ERROR.getErrCode());
31             responseData.put("errMsg",EmBusinessError.UNKNOW_ERROR.getErrMsg());
32         }
33         return CommonReturnType.create(responseData,"fail");
34     }
35 }

 

Guess you like

Origin www.cnblogs.com/wuba/p/11420597.html