0x000 概述:将某个http请求映射到某个方法上。0x001 @RequestMapping:这个注解可以加在某个Controller或者某个方法上,如果加在Controller上
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"; }
登录后复制精确匹配
@RequestMapping(value = "/hello2") public Object hello2() { return "hello2"; }
登录后复制字符模糊匹配
@RequestMapping(value = "/hello3/*") public Object hello3() { return "hello3"; }
登录后复制单字符模糊匹配
$ curl 127.0.0.1:8080/hello4/1 hello4
登录后复制全路径模糊匹配
@RequestMapping(value = "/hello5/**") public Object hello5() { return "hello5"; }
登录后复制多个路径匹配
@RequestMapping(value = {"/hello6", "/hello6/1"}) public Object hello6() { return "hello6"; }
登录后复制读取配置
// resources/application.properties app.name=hello7 // com.lyxxxx.rest.controller.HelloController @RequestMapping(value = "${app.name}") public Object hello7() { return "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 说明
以上参考数据:《Spring Boot2精髓 从构建小系统到架构分部署大系统》
相关文章:
相关视频:
以上就是怎么将某个http请求映射到某个方法上?SpringBoot入门:URL 映射的详细内容,更多请关注Work网其它相关文章!