SpringMVC global exception handling code
SpringMVC global exception handling code
Encapsulated object classes:
Package com. sun4j. app. exception;/*** status code and message entity class * @ author Sunlight **/public class CodeMessage {private String code; private String message; public CodeMessage () {} public CodeMessage (String code, String message) {this. setCode (code); this. setMessage (message);} public String getCode () {return code;} public void setCode (String code) {this. code = code;} public String getMessage () {return message;} public void setMessage (String message) {this. message = message ;}}
System exception:
Package com. sun4j. app. exception;/*** system exception class ** @ author Sunlight **/public class MyException extends RuntimeException {/*****/private static final long serialVersionUID = 1L; public static final MyException SUCCESS = new MyException (0, successful); public static final MyException SERVER_ERROR = new MyException (-1, server exception ); public static final MyException PERMISSION_DENIED = new MyException (-2, your permissions are insufficient); public static final MyException CONNECTION_TIMEOUT = new MyException (-3, network connection exception ); public static final MyException CONNECTION_ERROR = new MyException (-4, connection timeout); private String code; private String message; public MyException (String code, String message) {this. code = code; this. message = message;} public MyException (CodeMessage cm) {this. code = cm. getCode (); this. message = cm. getMessage () ;}public String getCode () {return code;} public void setCode (String code) {this. code = code;} public String getMessage () {return message;} public void setMessage (String message) {this. message = message;} public CodeMessage getCodeMessage () {return new CodeMessage (code, message );}}
Exception Handling class:
Package com. sun4j. app. exception; import java. io. IOException; import java. io. printWriter; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. springframework. http. mediaType; import org. springframework. web. bind. annotation. responseBody; import org. springframework. web. method. handlerMethod; import org. springframework. web. servlet. handlerExceptionResolver; import Org. springframework. web. servlet. modelAndView; import com. alibaba. fastjson. JSONObject;/*** Exception handling class ** @ author Sunlight **/public class implements attributes {@ Override public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {e. printStackTrace (); ModelAndView mv = new ModelAndView (); CodeMes Sage cm = null; if (e instanceof MyException) {cm = new CodeMessage (MyException) e ). getCode (), e. getMessage ();} else {cm = new CodeMessage (500, e. toString ();} mv. addObject (exception, cm); mv. addObject (resultCode, cm. getCode (); mv. addObject (resultMessage, unknown error); mv. setViewName (/err/500); HandlerMethod handlerMethod = (HandlerMethod) handler; ResponseBody body = handlerMethod. getMethodAnnotatio N (ResponseBody. class); if (body! = Null) {try {// return json data response. setContentType (MediaType. APPLICATION_JSON_VALUE); response. setCharacterEncoding (UTF-8); response. setHeader (Cache-Control, no-cache, must-revalidate); PrintWriter writer = response. getWriter (); writer. write (JSONObject. toJSONString (mv. getModel (); writer. close () ;}catch (IOException e1) {e1.printStackTrace () ;}} return mv ;}}