首页 > 编程笔记 > Java笔记 阅读:2

SpringBoot URL映射详解(附带实例)

将 HTTP 请求映射到具体方法,Spring Boot 支持 URL 路径匹配、HTTP Method 匹配、params 和 header 匹配等 URL 映射。

URL路径匹配

1) 精确匹配

@RequestMapping 的 value 属性用于匹配 URL 映射,value 支持简单表达式:
@RequestMapping("/getDataById/{id}")
public String getDataById(@PathVariable("id") Long id) {
    return "getDataById:"+id ;
}
在上面的示例中,@PathVariable 注解作用在方法参数中,用于表示参数的值来自 URL 路径。

如果 URL 中的参数名称与方法中的参数名称一致,则可以简化为:
@RequestMapping("/getDataById/{id}")
public String getDataById(@PathVariable Long id) {
    return "getDataById:"+id ;
}
在上面的示例中,当在浏览器中访问 /getDataById/1 时,会自动映射到后台的 getDataById 方法,传入参数 id 的值为 1。

2) 通配符匹配

@RequestMapping 支持使用通配符匹配 URL,用于统一映射某些 URL 规则类似的请求,示例代码如下:
@RequestMapping("/getJson/*.json")
public String getJson() {
    return "get json data";
}
在上面的示例中,当在浏览器中请求 /getJson/a.json 或者 /getJson/b.json 时都会匹配到后台的 Json 方法。

@RequestMapping 的通配符匹配非常简单实用,支持“*”、“?”、“**”等通配符。使用时需要了解通配符的匹配规则,否则容易出错。通配符匹配规则如下:

Method匹配

HTTP 请求 Method 有 GET、POST、PUT、DELETE 等方式。HTTP 支持的全部 Method 和说明如下表所示。

表:HTTP Method 说明
序号 HTTP Method 说明
1 GET 用于获取 URL 对应的数据
2 POST 用于提交后台数据
3 HEAD 类型为 GET,不返回消息体,用于返回对应 URL 的元信息
4 PUT 类型为 POST,对同一个数据多次进行 PUT 操作不会导致数据改变
5 DELETE 删除操作
6 PATCH 类似于 PUT 操作,表示信息的局部更新

对于 Web 应用,GET 和 POST 是经常使用的选项,而对于 RESTful 接口,则会使用 PUT、DELETE 等从语义上进一步区分操作。

@RequestMapping 注解提供了 method 参数指定请求的 Method 类型,包括 RequestMethod.GET、RequestMethod.POST、RequestMethod.DELETE、RequestMethod.PUT 等值,分别对应 HTTP 请求的 Method。

示例代码如下:
@RequestMapping(value="/getData",method = RequestMethod.GET)
public String getData() {
    return "RequestMethod GET";
}

@RequestMapping(value="/getData",method = RequestMethod.POST)
public String PostData() {
    return "RequestMethod POST";
}
上面的示例实现了 GET 和 POST 两种方式。当使用 GET 方式请求 /data/getData 接口时,会返回“RequestMethod GET”,使用 POST 方式请求 /data/getData 接口时,则返回“RequestMethod POST”,说明 @RequestMapping 通过 HTTP 请求 Method 映射不同的后台方法。

consumes和produces匹配

@RequestMapping 注解提供了 consumes 和 produces 参数用于验证 HTTP 请求的内容类型和返回类型:
下面通过示例演示 consumes 和 produces 参数的用法:
//处理request Content-Type为“application/json”类型的请求
@RequestMapping(value = "/Content", method = RequestMethod.POST, consumes = "application/json")
public String Consumes(@RequestBody Map param) {
    return "Consumes POST  Content-Type=application/json";
}
上面的示例只允许 Content-Type=application/json 的 HTTP 请求映射此方法,其他类型则返回“Unsupported Media Type”的错误。

params和header匹配

@RequestMapping 注解提供了 header 参数和 params 参数,通过 header 参数可以根据 HTTP 请求中的消息头内容映射 URL 请求,通过 params 参数可以匹配 HTTP 中的请求参数实现 URL 映射。

1) params

Spring Boot 除了通过匹配 URL 和 Method 的方式实现映射 HTTP 请求之外,还可以通过匹配 params 的方式来实现。

Spring Boot 从请求参数或 HTTP 头中提取参数,通过判断参数,如 params="action=save" 确定是否通过。同时还可以设置请求参数包含某个参数、不包含某个参数或者参数等于某个值时通过,具体如下:
通过 @PostMapping 设置的 params 参数来检查请求的 params,实现 HTTP 的 URL 映射。示例代码如下:
@RequestMapping(value="paramsTest",params="action=save")
public String paramsTest(@RequestBody Map param){
    return "params test";
}
在上面的示例中,当请求的参数 action=save 时,映射此方法。

2) header

header 的使用和 params 类似,它检查 HTTP 的 header 头中是否有 Host=localhost:8080 的参数,如果有则匹配此方法。示例代码如下:
@RequestMapping(value="headerTest",headers={"Host=localhost:8080"})
public String headerTest()
{
    return "header test";
}

相关文章