Exploring @ requestmapping in spring MVC

Source: Internet
Author: User

Continue to review @ requestmapping in spring MVC this time;

1) normal path
[Code = "Java"]

@ Requestmapping (value = "/foos ")
@ Responsebody
Public String getfoosbysimplepath (){
Return "get some foos ";
}
[/Code]

Then try to use curl to request
Curl-I http: // localhost: 8080/spring-MVC/foos

2) specify requestmethod. Post
[Code = "Java"]
@ Requestmapping (value = "/foos", method = requestmethod. Post)
@ Responsebody
Public String postfoos (){
Return "post some foos ";
}
[/Code]

Curl I-X Post http: // localhost: 8080/spring-MVC/foos

3) Specify the HTTP Request Header
[Code = "Java"]
@ Requestmapping (value = "/foos", headers = "Key = Val ")
@ Responsebody
Public String getfooswithheader (){
Return "get some foos with header ";
}

[/Code]
The headers can be followed by multiple headers, for example:
[Code = "Java"]
@ Requestmapping (value = "/foos", headers = {"key1 = val1", "key2 = val2 "})
@ Responsebody
Public String getfooswithheaders (){
Return "get some foos with header ";
}
[/Code]

Note that the curl request is:
Curl-I-h "key: Val" http: // localhost: 8080/spring-MVC/foos

4) @ new product and consume in requestmapping.
In spring 3.0, you can specify the media format of the Request Header, for example:
[Code = "Java"]
@ Requestmapping (value = "/foos", method = requestmethod. Get, headers = "Accept = application/JSON ")
@ Responsebody
Public String getfoosasjsonfrombrowser (){
Return "get some foos with header old ";
}
[/Code]

Curl test:
Curl-h "accept: Application/JSON, text/html" http: // localhost: 8080/spring-MVC/foos

If it is in 3.1, there will be new attributes of produces and consume, such:
[Code = "Java"]
@ Requestmapping (value = "/foos", method = requestmethod. Get, produces = "application/JSON ")
@ Responsebody
Public String getfoosasjsonfromrest (){
Return "get some foos with header new ";
}
[/Code]

If 3.1 is used, but the old method is still used, requests in the old method will automatically become produces and consume;

@ Requestmapping (value = "/testmsgconverter", consumes = "text/plain", produces = "application/JSON ")

Content-Type in the header of the request accepted by handlermethod is text/plain;
Accept is application/JSON

5) @ pathvariable
1 single
[Code = "Java"]
@ Requestmapping (value = "/foos/{ID }")
@ Responsebody
Public String getfoosbysimplepathwithpathvariable (@ pathvariable ("ID") Long ID ){
Return "get a specific Foo with ID =" + ID;
}
[/Code]

Test: curl http: // localhost: 8080/spring-MVC/foos/1

2 or more
[Code = "Java"]
@ Requestmapping (value = "/foos/{fooid}/BAR/{Barid }")
@ Responsebody
Public String getfoosbysimplepathwithpathvariables (@ pathvariable long fooid, @ pathvariable long Barid ){
Return "get a specific bar with ID =" + Barid + "from a foo with ID =" + fooid;
}
[/Code]

Curl http: // localhost: 8080/spring-MVC/foos/1/BAR/2
3. Regular Expressions are also supported.
[Code = "Java"]
@ Requestmapping (value = "/bars/{numericid: [\ D] + }")
@ Responsebody
Public String getbarsbysimplepathwithpathvariable (@ pathvariable final long numericid ){
Return "get a specific bar with ID =" + numericid;
}
[/Code]

The parameter only accepts numbers.

6) requestparam
[Code = "Java"]

Http: // localhost: 8080/spring-MVC/bars? Id = 100

@ Requestmapping (value = "/bars ")
@ Responsebody
Public String getbarbysimplepathwithrequestparam (@ requestparam ("ID") Long ID ){
Return "get a specific bar with ID =" + ID;
}

[/Code]
 
[Code = "Java"]
@ Requestmapping (value = "/bars", Params = "ID ")
@ Responsebody
Public String getbarbysimplepathwithexplicitrequestparam (@ requestparam ("ID") Long ID ){
Return "get a specific bar with ID =" + ID;
}
[/Code]

7) requestmapping supports ing multiple ing paths to the same controller, for example:
[Code = "Java"]
@ Requestmapping (value = {"/advanced/bars", "/advanced/foos "})
@ Responsebody
Public String getfoosorbarsbypath (){
Return "advanced-get some foos or bars ";
}
[/Code]

Curl-I http: // localhost: 8080/spring-MVC/advanced/foos
Curl-I http: // localhost: 8080/spring-MVC/advanced/bars

It even supports both put and post requests, such:

[Code = "Java"]

@ Requestmapping (value = "/foos/multiple", method = {requestmethod. Put, requestmethod. Post })
@ Responsebody
Public String putandpostfoos (){
Return "advanced-put and post within single method ";
}
[/Code]
 
Curl-I-X Post http: // localhost: 8080/spring-MVC/foos/multiple
Curl-I-x put http: // localhost: 8080/spring-MVC/foos/multiple

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.