Detailed explanation of spring MVC @ requestmapping annotation

introduction:

Some time ago, the rest style was used in the project to develop the program, but when submitting data in post and put modes, it was found that the server could not accept the submitted data (the server-side parameter binding did not add any annotation). It was checked that the submission method was application / JSON, and there was indeed data submitted by the browser in the data typed by the server through request. Getreader(). In order to find out the reason, we studied the parameter binding (@ requestparam, @ requestbody, @ requestheader, @ pathvariable), and looked at the relevant contents of httpmessageconverter, which are summarized here.

Introduction:

@RequestMapping

Requestmapping is an annotation used to handle request address mapping, which can be used on classes or methods. Used on a class to indicate that all methods in the class that respond to requests take this address as the parent path.

The requestmapping annotation has six attributes, which are divided into three categories for description.

1、 value, method;

Value: specify the actual address of the request. The specified address can be URI template mode (described later);

Method: Specifies the method type of the request, such as get, post, put, delete, etc;

2、 consumes,produces;

Consumers: specify the submitted content type for processing the request, such as application / JSON, text / HTML;

Products: Specifies the content type to be returned. It is returned only if the (accept) type in the request header contains the specified type;

3、 params,headers;

Params: Specifies that the request must contain some parameter values of yes before it can be processed by this method.

Headers: Specifies that the request must contain some specified header values in order for the method to process the request.

Example:

1. Value / method example

Default requestmapping ("... STR...") Is the value of value;

The URI values of value fall into the following three categories:

A) It can be specified as a common specific value;

B) Can be specified as a type of value containing a variable (URI template patterns with path variables);

C) Can be specified as a class of values with regular expressions (URI template patterns with regular expressions);

example B)

example C)

2. Examples of consumers and products

Examples of cousumes:

Method only processes requests with request content type of "application / JSON".

Examples of products:

Method only processes requests with "application / JSON" in the accept header of the request, and implies that the returned content type is application / JSON;

3. Examples of params and headers

Examples of params:

Only requests with the name "myparam" and the value "myvalue" are processed;

Example of headers:

The header that only processes the request contains the specified "refer" request header and the corresponding value is“ http://www.ifeng.com/ ”Request for;

The above only describes which requests are processed by the method specified by requestmapping. The next article will explain how to process the data submitted by the request (data binding) and the returned data.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>