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;@restcontrollerpublic class hellocontroller {}
添加方法,并添加url匹配:
@requestmapping() public object hello() { return "hello"; }
说明:上面添加了一个方法,名为hello,没有给定任何的属性,则默认匹配所有url,方法为get,所以我们可以直接使用get方式来访问该路由
$ curl 127.0.0.1:8080hello$ curl 127.0.0.1:8080/userhello$ curl 127.0.0.1:8080/user/1hello
精确匹配
@requestmapping(value = "/hello2")public object hello2() { return "hello2";}
说明:上面将value设置为hello2,所以访问hello2将会执行hello2方法
$ curl 127.0.0.1:8080/hello2hello2
字符模糊匹配
@requestmapping(value = "/hello3/*")public object hello3() { return "hello3";}
说明:上面将value设置为/hello3/*,*为匹配所有字符,也就是访问hello3下的所有url都将匹配该防范
$ curl 127.0.0.1:8080/hello3/userhello3 curl 127.0.0.1:8080/hello3/1hello3
单字符模糊匹配
$ curl 127.0.0.1:8080/hello4/1hello4
说明:上面将value设置为/hello4/?,?为匹配单个字符,匹配hello4/1,但是不会匹配hello4/11
$ curl 127.0.0.1:8080/hello4/1hello4$ 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/hello5hello5$ curl 127.0.0.1:8080/hello5/user/1hello5
多个路径匹配
@requestmapping(value = {"/hello6", "/hello6/1"})public object hello6() { return "hello6";}
$ curl 127.0.0.1:8080/hello6hello6$ curl 127.0.0.1:8080/hello6/1hello6f
读取配置
// resources/application.propertiesapp.name=hello7// com.lyxxxx.rest.controller.hellocontroller@requestmapping(value = "${app.name}")public object hello7() { return "hello7";}
$ curl 127.0.0.1:8080/hello7hello7
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;@restcontrollerpublic 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/getget$ curl -x post 127.0.0.1:8080/method/postpost$ curl -x delete 127.0.0.1:8080/method/deletedelete$ curl -x put 127.0.0.1:8080/method/putput...
0x003 params 匹配除了可以匹配url和method之外,还可以匹配params
package com.lyxxxx.rest.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.restcontroller;@restcontrollerpublic class paramscontroller { @requestmapping(path = "/params", params = "userid=1") public object params() { return "params"; }}
$ curl 127.0.0.1:8080/params?userid=1params
0x004 说明以上参考数据:《spring boot2精髓 从构建小系统到架构分部署大系统》
相关文章:
mybatis入门基础(四)----输入映射和输出映射
django中“url映射规则”和“服务端响应顺序”
相关视频:
css3 入门教程
以上就是怎么将某个http请求映射到某个方法上?springboot入门:url 映射的详细内容。