0x000 概述:將某個http請求對應到某個方法上。 0x001 @RequestMapping:這個註解可以加在某個Controller或某個方法上,如果加在Controller上
將某個#http
請求對應到某個方法上
@RequestMapping
這個註解可以加在某個Controller
或某個方法上,如果加在Controller
上,則這個Controller
中的所有路由映射都會加上這個前綴(下面會有栗子),如果加在方法上,則沒有前綴(下面也有栗子)。
@RequestMapping
有以下屬性
#value
: 請求的URL
的路徑
path
: 和value
一樣
method
: 請求的方法
consumes
: 允許的媒體類型,也就是Content-Type
# #produces: 對應的媒體類型,也就是
Accept
#params: 請求參數
0x002 路由匹配
##添加方法,並新增先寫Controller
,
Controller頭部的
@RestController將這個控制器標註為
Rest控制器:
package com.lyxxxx.rest.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { }登入後複製
@RequestMapping() public Object hello() { return "hello"; }
說明:上面新增了一個方法,名為hello
,沒有給定任何的屬性,則預設符合所有
URL,方法為
GET,所以我們可以直接使用
GET方式來存取該路由
$ curl 127.0.0.1:8080 hello $ curl 127.0.0.1:8080/user hello $ curl 127.0.0.1:8080/user/1 hello登入後複製
@RequestMapping(value = "/hello2") public Object hello2() { return "hello2"; }
字元模糊匹配說明:上面將value
設定為
hello2,所以訪問
hello2將會執行
hello2方法
$ curl 127.0.0.1:8080/hello2 hello2登入後複製
@RequestMapping(value = "/hello3/*") public Object hello3() { return "hello3"; }
:上面將#單字元模糊匹配value設定為/hello3/*
,
*為匹配所有字符,也就是存取
hello3下的所有
URL都會匹配該防範
$ curl 127.0.0.1:8080/hello3/user hello3 curl 127.0.0.1:8080/hello3/1 hello3登入後複製
$ curl 127.0.0.1:8080/hello4/1 hello4
##說明
#:上面將value設定為/hello4/?,
?
為符合單一字符,符合hello4/1
,但不會匹配hello4/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">登入後複製</div></div>
@RequestMapping(value = "/hello5/**") public Object hello5() { return "hello5"; }
說明:上面將
/hello5/**
,**為符合所有路徑,所以
$ curl 127.0.0.1:8080/hello5 hello5 $ curl 127.0.0.1:8080/hello5/user/1 hello5
多個路徑匹配
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">@RequestMapping(value = {"/hello6", "/hello6/1"})
public Object hello6() {
return "hello6";
}</pre><div class="contentsignin">登入後複製</div></div>
$ curl 127.0.0.1:8080/hello6 hello6 $ curl 127.0.0.1:8080/hello6/1 hello6F
讀取配置<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">// resources/application.properties
app.name=hello7
// com.lyxxxx.rest.controller.HelloController
@RequestMapping(value = "${app.name}")
public Object hello7() {
return "hello7";
}</pre><div class="contentsignin">登入後複製</div></div>
$ curl 127.0.0.1:8080/hello7 hello7
0x003 方法符合符合要求中的
中的列舉值:
#POST
PUT
#PATCH
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 ...
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
以上是怎麼將某個http請求對應到某個方法上? SpringBoot入門:URL 映射的詳細內容。更多資訊請關注PHP中文網其他相關文章!