0x000 概述:将某个http请求映射到某个方法上。0x001 @requestmapping:这个注解可以加在某个controller或者某个方法上,如果加在controller上
将某个http请求映射到某个方法上
@RequestMapping
这个注解可以加在某个Controller或者某个方法上,如果加在Controller上,则这个Controller中的所有路由映射都将会加上这个前缀(下面会有栗子),如果加在方法上,则没有前缀(下面也有栗子)。
@RequestMapping有以下属性
value: 请求的URL的路径
path: 和value一样
method: 请求的方法
consumes: 允许的媒体类型,也就是Content-Type
produces: 相应的媒体类型,也就是Accept
params: 请求参数
headers: 请求头部
首先编写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登录后复制
匹配请求中的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 ...
除了可以匹配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
以上参考数据:《Spring Boot2精髓 从构建小系统到架构分部署大系统》
相关文章:
相关视频:
以上就是怎么将某个http请求映射到某个方法上?SpringBoot入门:URL 映射的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号