System framework of the early stage of the process, in order to restrain the code specification, we will do some with some common functions, such as public statement some system errors, encapsulate common return result, unified exception handling, and so on, the advantage is in the process of team development to form a unified code specification, enhance readability, and also facilitate the late code maintenance. This paper mainly introduces the processing of exceptions in the framework:

1. Declare the global exception handling class and add the @ControllerAdvice and @RestController annotations as follows:

@RestController public class GlobalExceptionHandler { private final Logger logger = Logger.getLogger(GlobalExceptionHandler.class); / / null pointer exception @ ExceptionHandler (NullPointerException. Class) public Map < String, Object> nullPointer(NullPointerException e,HttpServletRequest req){ logger.error(e.getMessage()); e.printStackTrace(); return ResultObject.newResultObj(ErrorCode.NULLPOINTER); } / / IO exception @ ExceptionHandler (IOException. Class, it the req, HttpServletResponse res) public Map < String, Object> IOException(IOException e){ logger.error(e.getMessage()); e.printStackTrace(); return ResultObject.newResultObj(ErrorCode.IOEXCEPTION); } / / insufficient permissions abnormal @ ExceptionHandler (UnauthorizedException. Class) public Map < String, Object > unauth (it the req. HttpServletResponse res){ return ResultObject.newResultObj(ErrorCode.UNAUTH); } / / not the abnormal @ ExceptionHandler (AuthorizationException. Class) public Map < String, Object > unlogin (it the req, HttpServletResponse res){ return ResultObject.newResultObj(ErrorCode.NOTLOGIN); } @ExceptionHandler(exception.class) public Map<String, Object> error(Exception e,HttpServletRequest req, HttpServletResponse res){ logger.error(e.getMessage()); e.printStackTrace(); return ResultObject.newResultObj(ErrorCode.SYSTEM); }}Copy the code

2. Declare a common error handling class

*/ public class ErrorCode {private int code = 0; // Description private String errmsg = "SUCCESS"; Public static final ErrorCode SUCCESS = new ErrorCode(0,"SUCCESS"); Public static final ErrorCode PARAMS = new ErrorCode(100000," error "); Public static final ErrorCode SYSTEM = new ErrorCode(100001," SYSTEM error "); Public static final ErrorCode UNLOGIN = new ErrorCode(100002," Not logged in "); Public static final ErrorCode UNAUTH = new ErrorCode(100003," Permission error "); Public static final ErrorCode NULLPOINTER = new ErrorCode(100004," NULLPOINTER error "); Public static final ErrorCode IOEXCEPTION = new ErrorCode(100005,"I/O error "); Public static final ErrorCode UPLOAD_FAIL = new ErrorCode(100006," Failed to upload file "); Public static final ErrorCode REPEAT = new ErrorCode(100007," Data already exists "); public ErrorCode() {} @Override public String toString() { return String.format("{\"errmsg\": \"%s\", \"code\": %d}", errmsg, code); } public int getCode() { return code; } public ErrorCode setCode(int code) { this.code = code; return this; } public String getErrmsg() { return errmsg; } public ErrorCode setErrmsg(String errmsg) { this.errmsg = errmsg; return this; } public ErrorCode(int code, String errmsg) { this.code = code; this.errmsg = errmsg; }}Copy the code

3. Declare the result return class in the global exception handling class

import java.util.Map; import javax.servlet.http.HttpServletResponse; import com.xxx.ErrorCode; Import org.json.jsonObject; import org.json.jsonObject; import org.json.jsonObject; import org.json.jsonObject; Public class ResultObject {/** * return error message * @param errorCode */ public static Map<String, Object> newResultObj(ErrorCode errorCode){ Map<String, Object> obj = new HashMap<String, Object>(); obj.put("code", errorCode.getCode()); obj.put("errmsg", errorCode.getErrmsg()); return obj; } /** * public static Map<String, Object> newResultObj(Object data){Map<String, Object> obj = new HashMap<String, Object>(); obj.put("code", ErrorCode.SUCCESS.getCode()); obj.put("errmsg", ErrorCode.SUCCESS.getErrmsg()); if(null ! = data){ obj.put("data", data); } return obj; } @param errorCode @param data */ public static Map<String, Object> newResultObj(ErrorCode errorCode, Object data){ Map<String, Object> obj = new HashMap<String, Object>(); obj.put("code", errorCode.getCode()); obj.put("errmsg", errorCode.getErrmsg()); if(null ! = data){ obj.put("data", data); } return obj; Public static void outputResult(HttpServletResponse Response, ErrorCode errorCode){ try { JSONObject json = new JSONObject(); json.put("code", errorCode.getCode()); json.put("errmsg", errorCode.getErrmsg()); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=UTF-8"); response.getWriter().write(json.toString()); } catch (Exception e1) { } } }Copy the code