Then the previous article, "SPRINGMVC returns to the client by exception enhancement Unified format" through the spring Controlleradvice the various exceptions to intercept processing, the unified format returned to the client.
Next we are more elaborate, through the @exceptionhandler intercept exception, the parameters of the client parameter is not passed or inconsistent parameter data type, convenient client-side service end-Test.
Briefly describe the main process of interception exception:
1. Customize a class Restexceptionhandler and use the @controlleradvice annotation to indicate that this class is a controller enhancement;
2. Create a new method in Restexceptionhandler and use the @exceptionhandler ({EXCEPTION.CLSS}) annotation on the method to indicate that the method handles exception information.
3. Configure in Springmvc.xml
<type= "annotation" expression= " Org.springframework.web.bind.annotation.ControllerAdvice "/>
@ExceptionHandler annotations Allow us to specify the type of exception to intercept, or to intercept custom exceptions.
So let's take a look at the exception type of the SPRINGMVC for HTTP requests.
Exception Type |
HTTP Status Code |
Conversionnotsupportedexception |
$ (Internal Server Error) |
Httpmediatypenotacceptableexception |
406 (not acceptable) |
Httpmediatypenotsupportedexception |
415 (Unsupported Media Type) |
Httpmessagenotreadableexception |
(Bad Request) |
Httpmessagenotwritableexception |
$ (Internal Server Error) |
Httprequestmethodnotsupportedexception |
405 (Method not allowed) |
Missingservletrequestparameterexception |
(Bad Request) |
Nosuchrequesthandlingmethodexception |
404 (Not Found) |
Typemismatchexception |
(Bad Request) |
SPRINGMVC has defined the common exception types for HTTP requests for us, we only need to use @exceptionhandler ({missingservletrequestparameterexception.class}) Note on a method, the method parameter type is the exception type that we specify, and we can get the exception object when the parameter exception is missing.
Parameter type mismatch//getpropertyname () Gets the data type mismatch parameter name//getrequiredtype () actually requires the client to pass the data type @exceptionhandler ({ Typemismatchexception.class}) @ResponseBodypublic String Requesttypemismatch (Typemismatchexception ex) { Ex.printstacktrace (); return Outputjson (-400, "parameter type mismatch, parameter" + ex.getpropertyname () + "type should be" + ex.getrequiredtype ());} Missing parameter Exception//getparametername () missing parameter name @exceptionhandler ({missingservletrequestparameterexception.class}) @ Responsebodypublic String Requestmissingservletrequest (Missingservletrequestparameterexception ex) { Ex.printstacktrace (); return Outputjson (-400, "missing necessary parameters, parameter name" + Ex.getparametername ());}
This way, whether it is a parameter exception, a data type exception, or a request method exception, can be fine processing, accurate to a method of parameters and data type, to the client prompt more meaningful information.
SPRINGMVC Request Parameter Exception handling