0x000 Overview: Map an http request to a method. 0x001 @RequestMapping: This annotation can be added to a Controller or a method. If added to a Controller
Map a http
request to On a certain method
@RequestMapping
This annotation can be added to a certain Controller
or a certain method. If added to Controller
, then all route mappings in this Controller
will be added with this prefix (there will be chestnuts below). If it is added to the method, there will be no prefix (there will be chestnuts below).
@RequestMapping
has the following attributes
value
: The path of the requested URL
path
: Same as value
method
: Requested method
consumes
: Allowed media types, that is, Content-Type
produces
: The corresponding media type, that is, Accept
##params: Request parameters
headers: Request headers
Controller,
Controller@RestController
in the header marks this controller as
RestController:
package com.lyxxxx.rest.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { }
URL to match:
@RequestMapping() public Object hello() { return "hello"; }
Description: The above adds a method named hello
, which is not given Any attribute will match all
URLby default, and the method is
GET, so we can directly use the
GETmethod to access the route
$ curl 127.0.0.1:8080 hello $ curl 127.0.0.1:8080/user hello $ curl 127.0.0.1:8080/user/1 helloCopy after login
@RequestMapping(value = "/hello2") public Object hello2() { return "hello2"; }
Explanation: The above sets value
to
hello2, so access
hello2will execute
hello2method
$ curl 127.0.0.1:8080/hello2 hello2Copy after login
@RequestMapping(value = "/hello3/*") public Object hello3() { return "hello3"; }
Note: The above value is set to
/hello3/*
,*
is to match all characters, that is, accesshello3
AllURL
will match this prevention<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$ curl 127.0.0.1:8080/hello3/user hello3 curl 127.0.0.1:8080/hello3/1 hello3</pre><div class="contentsignin">Copy after login</div></div>
$ curl 127.0.0.1:8080/hello4/1 hello4
Description: The above sets value to
/hello4/?
,?
matches a single character, and matcheshello4/1
, but it does not Matchhello4/11
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$ curl 127.0.0.1:8080/hello4/1 hello4 $ curl 127.0.0.1:8080/hello4/12 {"timestamp":"2018-07-25T05:29:39.105+0000","status":404,"error":"Not Found","message":"No message available","path":"/hello4/12"}</pre><div class="contentsignin">Copy after login</div></div>
@RequestMapping(value = "/hello5/**") public Object hello5() { return "hello5"; }
Explanation: The above will value is set to
/hello5/**
,**
is to match all paths, so all routes belowhello5
will match this Method<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$ curl 127.0.0.1:8080/hello5 hello5 $ curl 127.0.0.1:8080/hello5/user/1 hello5</pre><div class="contentsignin">Copy after login</div></div>
@RequestMapping(value = {"/hello6", "/hello6/1"}) public Object hello6() { return "hello6"; }
$ curl 127.0.0.1:8080/hello6 hello6 $ curl 127.0.0.1:8080/hello6/1 hello6F
// resources/application.properties app.name=hello7 // com.lyxxxx.rest.controller.HelloController @RequestMapping(value = "${app.name}") public Object hello7() { return "hello7"; }
$ curl 127.0.0.1:8080/hello7 hello7
in the request, the enumeration value written in RequestMethod
:
package com.lyxxxx.rest.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MethodController { @RequestMapping(path = "method/get", method = RequestMethod.GET) public Object get() { return "get"; } @RequestMapping(path = "method/head", method = RequestMethod.HEAD) public Object head() { return "head"; } @RequestMapping(path = "method/post", method = RequestMethod.POST) public Object post() { return "post"; } @RequestMapping(path = "method/put", method = RequestMethod.PUT) public Object put() { return "put"; } @RequestMapping(path = "method/patch", method = RequestMethod.PATCH) public Object patch() { return "patch"; } @RequestMapping(path = "method/delete", method = RequestMethod.DELETE) public Object delete() { return "delete"; } @RequestMapping(path = "method/options", method = RequestMethod.OPTIONS) public Object options() { return "options"; } @RequestMapping(path = "method/trace", method = RequestMethod.TRACE) public Object trace() { return "trace"; } }
$ curl -X GET 127.0.0.1:8080/method/get get $ curl -X POST 127.0.0.1:8080/method/post post $ curl -X DELETE 127.0.0.1:8080/method/delete delete $ curl -X PUT 127.0.0.1:8080/method/put put ...
method, it can also match
params
package com.lyxxxx.rest.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ParamsController { @RequestMapping(path = "/params", params = "userId=1") public Object params() { return "params"; } }
$ curl 127.0.0.1:8080/params?userId=1 params
0x004 Description
The above reference data: "The essence of Spring Boot2 from building a small system to architecture and deploying a large system"Related articles:
MyBatis Getting Started Basics (4)----Input Mapping and Output Mapping
"url mapping rules" and "server response order" in django
CSS3 Getting Started Tutorial
The above is the detailed content of How to map an http request to a method? Getting Started with SpringBoot: URL Mapping. For more information, please follow other related articles on the PHP Chinese website!