@RequestParam @RequestBody @PathVariable and other parameter bindings __spring

Source: Internet
Author: User
Introduction:

After an article on @requestmapping address mapping, the main explanation of the request data to handler method parameter data binding to use the annotation and what circumstances use;


Introduction:

Handler method parameter binding commonly used annotations, we are divided into four categories according to the different contents of the request they handle: (mainly to explain the common types)

A, processing Requet URI part (here refers to the URI template variable, does not contain QueryString part) annotation: @PathVariable;

B, processing request header part of the note: @RequestHeader, @CookieValue;

C, processing request the body part of the annotation: @RequestParam, @RequestBody;

D, processing attribute types are annotations: @SessionAttributes, @ModelAttribute;

1, @PathVariable

When using the @requestmapping URI template The style map, that is, someurl/{paramid}, the Paramid can pass the value passed by @Pathvariable annotation to the parameter of the method.

Sample code: [Java] view plain copy? @Controller @RequestMapping ("/owners/{ownerid}") public class Relativepathuritemplatecontroller {@RequestMappi           Ng ("/pets/{petid}") public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) { Implementation omitted} The code above binds the value of the variable ownerid in the URI template to the parameter of the method. If the name of the method parameter does not match the name of the variable in the URI template that you want to bind, you need to specify the name in the URI template in @pathvariable ("name").

2, @RequestHeader, @CookieValue

@RequestHeader annotation, you can bind the value of the header part of request requests to the parameters of the method.

Sample code:

This is the header portion of a request:[Plain]View plain copy? Host localhost:8080 Accept text/html,application/xhtml+xml,application/xml;q=0.9 A Ccept-language fr,en-gb;q=0.7,en;q=0.3 accept-encoding gzip,deflate accept-charset ISO-8859-1 , utf-8;q=0.7,*;q=0.7 keep-alive 300
[Java]View plain copy? @RequestMapping ("/displayheaderinfo.do") public void Displayheaderinfo (@RequestHeader ("accept-encoding") String   Encoding, @RequestHeader ("keep-alive") long keepAlive) {//...} The code above, which binds the accept-encoding value of the request header part to the parameter encoding, keep-alive the value of the header to the parameter keepalive.


@CookieValue can bind the value of a cookie in the request header to the parameters of the method.

For example, there are the following cookie values:
[Java] view plain copy? Code for jsessionid=415a4ac178c59dace0b2c9ca727cdd84 parameter bindings: [Java] view plain copy?        @RequestMapping ("/displayheaderinfo.do") public void Displayheaderinfo (@CookieValue ("Jsessionid") String cookie) {   //...      } That is, the value of the Jsessionid is bound to the parameter cookie.


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.