This paper mainly combines the Restfuljava backend and summarizes the exception processing architecture.
1. Classification and processing methods of exception
Exception is a very important feature of object-oriented language, and good exception design is of great importance to the expansibility, maintainability and robustness of the program.
Java defines two types of exceptions based on their usefulness
Checked Exception:exception Subclass, the method signature needs to be displayed on the declaration throws, the compiler forces the caller to handle such exceptions or declare throws to continue to throw up.
Unchecked Exception:runtimeexception subclass, the method signature does not need to be declared throws, and the compiler does not force the caller to handle the class exception.
What we're going to talk about is checkedexception:
Application-Level exception: The exception that are thrown by the application during operation are mainly related to the business logic of the application, and these exception are defined in the application, such as: the input login account is not found, login failed.
System-Level exception: the exceptions that the system throws during the run, such as IO errors, arithmetic errors, and so on.
The only thing I want to talk about is the handling of these two types of exception.
2. Centralized exception handler how to define and use
System Exception:exception and SQL Exception
Business Exception:badrequestexception, Notfoundexception,serverrejectexception, SystemException
These Exception can be thrown anywhere in the restful backend system, with the spring Exception handler to centralize all the thrown Exception
@ControllerAdvicepublic class Globalexceptioncontroller {private static final Log logger = Logfactory.getlog ( Globalexceptioncontroller.class); @ExceptionHandler (Sqlexception.class) @ResponseStatus (value = httpstatus.internal_server_error) public Modelandview handlesqlexception (HttpServletRequest request,SQLException ex ) {Handlelog (request, ex); map<string, object> errormap = new hashmap<string, object> (); Errormap.put ("Code", Httpstatus.internal_ Server_error); Errormap.put ("Requested", Request.getrequesturl ()); Errormap.put ("Message", ex.tostring ()); return New Modelandview (Jsonutils.view_name,jsonstringview.json_model_data, errormap);} @ExceptionHandler (Badrequestexception.class) @ResponseStatus (value = httpstatus.bad_request) public Modelandview Handlebadrequestexception (HttpServletRequest request, badrequestexception ex) {handlelog (request, ex); Exceptionmodel Exceptionmodel = Getexceptionmodel (Httpstatus.bad_request, ex); return new Modelandview (JSONUtils.VIEW _name,jsonstringview.jsOn_model_data, Exceptionmodel);} @ExceptionHandler (Serverrejectexception.class) @ResponseStatus (value = httpstatus.forbidden) public Modelandview Handleserverrejectexception (HttpServletRequest request, serverrejectexception ex) {handlelog (request, ex); Exceptionmodel Exceptionmodel = Getexceptionmodel (Httpstatus.forbidden,ex); return new Modelandview (JSONUtils.VIEW_ Name,jsonstringview.json_model_data, Exceptionmodel);} @ExceptionHandler (Notfoundexception.class) @ResponseStatus (value = httpstatus.not_found) public Modelandview Handlenotfoundexception (HttpServletRequest request,notfoundexception ex) {handlelog (request, ex); Exceptionmodel Exceptionmodel = Getexceptionmodel (Httpstatus.not_found,ex); return new Modelandview (JSONUtils.VIEW_ Name,jsonstringview.json_model_data, Exceptionmodel);} @ExceptionHandler (Systemexception.class) @ResponseStatus (value = httpstatus.internal_server_error) public Modelandview handlesystemexception (HttpServletRequest request,systemexception ex) {handlelog (request, ex); EXceptionmodel Exceptionmodel = Getexceptionmodel (Httpstatus.internal_server_error, ex); return new ModelAndView ( Jsonutils.view_name,jsonstringview.json_model_data, Exceptionmodel);} @ExceptionHandler (Exception.class) @ResponseStatus (value = httpstatus.internal_server_error) public Modelandview Handleallexception (HttpServletRequest request,exception ex) {handlelog (request, ex); map<string, object> errormap = new hashmap<string, object> (); Errormap.put ("Code", Integer.valueof ( HttpStatus.INTERNAL_SERVER_ERROR.value ())); Errormap.put ("Message", ex.tostring ()); return new Modelandview ( Jsonutils.view_name,jsonstringview.json_model_data, Errormap);} Private Exceptionmodel Getexceptionmodel (Httpstatus httpstatus,commonexception ex) {Exceptionmodel ExceptionModel = New Exceptionmodel (); Errorenum errorenum = Ex.geterrorenum (); Exceptionmodel.setstatus (Httpstatus.value ()); Exceptionmodel.setmoreinfo ( Ex.getmoreinfo ()); if (errorenum! = null) {Exceptionmodel.seterrorcode (Errorenum.getcode ()); ExceptiOnmodel.setmessage (Errorenum.tostring ());} return Exceptionmodel;} private void Handlelog (HttpServletRequest request, Exception ex) {Map parameter = Request.getparametermap (); StringBuffer logbuffer = new StringBuffer (), if (Request! = null) {logbuffer.append ("request Method=" + Request.getmethod ()); Logbuffer.append ("Url=" + Request.getrequesturl ());} if (ex instanceof commonexception) {logbuffer.append ("moreinfo=" + ((commonexception) ex). Getmoreinfo ());} if (ex! = null) {Logbuffer.append ("exception:" + ex);} Logger.error (Logbuffer.tostring ());}}
If you want to know more, you can refer to this
http://www.journaldev.com/2651/ Spring-mvc-exception-handling-exceptionhandler-controlleradvice-handlerexceptionresolver-json-response-example
3. Exception return design for HTTP request
Design different return messages and ErrorCode in each app exception:
And the returned status is also different.
There are two different ways to return errors for restful interface:
The first kind of Facebook, whether returned by normal return or error, is the Httpstatus, which returns information that requires check return object
The second Twilio and SimpleGeo, HTTP status returns 200 normally, error returns 400,404, 403, and so on.
The author adopts the second method.
The return value of the Http status selection:
Based on the specific situation of restful applications, the author mainly chooses:
· 200–ok
· 400-bad Request
· 403–forbidden
· 404–not Found
· 503–internal Servererror
Restful returns the model class definition for the error message:
Public Classexceptionmodel { private Integer status; Private Integer ErrorCode; private String message; Private String moreinfo; Public Integer GetErrorCode () { return errorCode; } public void Seterrorcode (Integer errorCode) { this.errorcode = ErrorCode; } Public Integer GetStatus () { return status; } public void SetStatus (Integer status) { this.status = status; } Public String GetMessage () { return message; } public void Setmessage (String message) { this.message = message; } Public String Getmoreinfo () { return moreinfo; } public void Setmoreinfo (String moreinfo) { this.moreinfo = moreinfo; }}
{"Message": "400202:the specific parameter is exist in System.", "status": +, "ErrorCode": 400202, "moreinfo": " 06d690e7-fb4d-45a8-91e0-7d82b0c60ca5 and 09f5edd6-1be3-4949-a302-9f487f82d723 ' is exist in system '}
For details, please refer to:
Https://blog.apigee.com/detail/restful_api_design_what_about_errors
4. Input and return validation using spring AOP
@Aspectpublic class Infoobjectinterceptor {@Autowiredprivate Validatorbean validatorbean; private static final Log Logge R =logfactory . GetLog (Infoobjectinterceptor.class); @Pointcut ("Execution (* com.json.BeanLang.createInfoObject (..))") Private Voidcreateinfoobjectmethod () {}// @Before ("Createinfoobjectmethod () && args (Infoobjectmodel)") Public Voiddoaccesscreatecheck (Infoobjectmodel Infoobjectmodel) throws Exception { if ( Validatorbean.checkinfoobjectexist (Infoobjecid)) { String moreinfo = Newstringbuffer () . Append (infoobjecid ) . Append ("Isexist in System"). ToString (); Throw newbadrequestexception ( errorenum.parameter_exist_in_system,moreinfo);}}
Spring AOP can be consulted:
http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/
5. Principles and techniques for exception handling
Reference: http://lavasoft.blog.51cto.com/62575/18920/
1, avoid too large try block, do not put the code will not appear in the try block, try to keep a try block corresponding to one or more exceptions.
2, refine the type of the exception, not regardless of what type of exception are written excetpion.
3, catch block as far as possible to keep a block catch a class of exceptions, do not ignore the caught exception, capture to either process, or translate, or re-throw the new type of exception.
4. Don't throw the exception that you can handle to others.
5, do not use Try...catch to participate in control procedures, the fundamental purpose of exception control is to deal with the abnormal situation of the program
6. Advantages
A) Business information feedback to the client, convenient system integration and debugging
b) System-level exception facilitates the discovery of hidden bugs in the function test
Reference:
http://mariuszprzydatek.com/2013/07/29/spring-localized-exception-handling-in-rest-api/
A summary of the processing architecture of Java restful back-end Exception