0x000 概述
将某个http
请求映射到某个方法上
0x001 @RequestMapping
这个注解可以加在某个Controller
或者某个方法上,如果加在Controller
上,则这个Controller
中的所有路由映射都将会加上这个前缀(下面会有栗子),如果加在方法上,则没有前缀(下面也有栗子)。
@RequestMapping
有以下属性
-
value
: 请求的URL
的路径 -
path
: 和value
一样 -
method
: 请求的方法 -
consumes
: 允许的媒体类型,也就是Content-Type
-
produces
: 相应的媒体类型,也就是Accept
-
params
: 请求参数 -
headers
: 请求头部
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 { }
-
添加方法,并添加
URL
匹配:@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
$ 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"}
-
全路径模糊匹配
@RequestMapping(value = "/hello5/**") public Object hello5() { return "hello5"; }
说明:上面将
value
设置为/hello5/**
,**
为匹配所有路径,所以hello5
下面的所有路由都将匹配这个方法$ curl 127.0.0.1:8080/hello5 hello5 $ curl 127.0.0.1:8080/hello5/user/1 hello5
-
多个路径匹配
@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
0x003 方法匹配
匹配请求中的method
,写在RequestMethod
中的枚举值:
GET
HEAD
POST
PUT
PATCH
DELETE
OPTIONS
TRACE
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
...
0x003 params 匹配
除了可以匹配URL
和method
之外,还可以匹配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 header 匹配
@RestController
public class HeaderController {
@RequestMapping(path = "/headers", headers = "userId=1")
public Object headers() {
return "headers";
}
}
$ curl 127.0.0.1:8080/headers
{"timestamp":"2018-08-01T06:11:40.037+0000","status":404,"error":"Not Found","message":"No message available","path":"/headers"}
$ curl 127.0.0.1:8080/headers -H "userId:1"
headers
0x005 consumes 匹配
@RestController
public class ConsumesController {
@RequestMapping(value = "consumes",consumes = "application/json")
public Object json() {
return "consumes";
}
}
$ curl 127.0.0.1:8080/consumes -H "Content-Type:application/json"
consumes
$ curl 127.0.0.1:8080/consumes -H "Content-Type:none"
{"timestamp":"2018-08-01T06:15:10.919+0000","status":415,"error":"Unsupported Media Type","message":"Invalid mime type \"none\": does not contain '/'","path":"/consumes"}
0x006 produces 匹配
@RestController
public class ProducesController {
@RequestMapping(value = "/produces", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object produces() {
return "produces";
}
}
$ curl 127.0.0.1:8080/produces
produces
$ curl 127.0.0.1:8080/produces -H "Accept:text"
// 空
0x007 说明
以上参考数据:《Spring Boot2精髓 从构建小系统到架构分部署大系统》
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。