SPRINGMVC Request Parameter Exception handling

Source: Internet
Author: User

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

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.