SpringMVC global exception handling code

Source: Internet
Author: User

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 ;}}

 

 
  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.