RequestMapping详解
属性详解
path 请求路径
method 规定请求方式
//规定请求方式
@RequestMapping(path = "/hello02", method = RequestMethod.POST)
public String requestMethod() {
System.out.println("requestMethod...");
return "success";
}
params 规定请求参数
/**
* params = {"username"} 必须带username字段
* params = {"!username"} 必须不能带username字段
* params = {"username=123"} 必须带username字段且值为123
* params = {"username!=123"} 不带username字段或者username字段值不是123
* params = {"username!=123","age"} 不带username字段或者username字段值不是123且必须带age字段
*/
//规定请求参数
@RequestMapping(path = "/hello03", params = {"username!=123", "age"})
public String requestParams() {
System.out.println("requestParams");
return "success";
}
headers 规定请求头信息
//规定请求头
@RequestMapping(path = "/hello04", headers = {"User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36"})
public String requestHeaders() {
System.out.println("requestParams");
return "success";
}
consumes 规定请求Conent-Type
produces 规定响应的Conent-Type
ant风格路径匹配
? 匹配一个字符,0个多个都不行
@RequestMapping("antTest0?")
* 匹配任意多个字符
@RequestMapping("antTest0*")
@RequestMapping("/a*/antTest04")
@RequestMapping("/a*/antTest0*")
** 匹配任意多层路径
@RequestMapping("/a/**/antTest0*")
Rest风格的URL请求
PathVariable注解的使用
获取url路径上的某称路径的值
// 只能占一层路径
@RequestMapping("/user/{id}")
public String antTest06(@PathVariable("id") String id) {
System.out.println(id);
return "success";
}
REST(Representational State Transfer) 资源表现层状态转换
以简洁的方式提交请求,用请求方式区分对资源操作
后端代码
主要是要设置method的请求方式
@RequestMapping(path = "/book/{id}", method = RequestMethod.POST)
public String addBook(@PathVariable("id") String id) {
System.out.println("添加" + id + "图书");
return "success";
}
@RequestMapping(path = "/book/{id}", method = RequestMethod.DELETE)
public String deleteBook(@PathVariable("id") String id) {
System.out.println("删除" + id + "图书");
return "success";
}
@RequestMapping(path = "/book/{id}", method = RequestMethod.PUT)
public String updateBook(@PathVariable("id") String id) {
System.out.println("修改" + id + "图书");
return "success";
}
@RequestMapping(path = "/book/{id}", method = RequestMethod.GET)
public String getBook(@PathVariable("id") String id) {
System.out.println("查询" + id + "图书");
return "success";
}
前端代码
除了get请求外,其他三种请求方式均要通过post来请求,对于put和delete,还需要设置一个_method字段,其值为对应的请求方式
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2021/2/15
Time: 16:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<body>
<!--
发起图书的增删改查请求:使用REST风格的URL地址
-->
<!--post方式-->
<form action="book/1" method="post">
<input type="submit" value="添加图书">
</form>
<!--delete方式-->
<form action="book/1" method="post">
<input name="_method" value="delete">
<input type="submit" value="删除图书">
</form>
<!--put方式-->
<form action="book/1" method="post">
<input name="_method" value="put">
<input type="submit" value="修改图书">
</form>
<!--get方式-->
<a href="book/1">查询图书</a>
</body>
</body>
</html>
设置过滤器
通过以上方式设置完成后对于get请求和post请求来说可以,但对于delete和put来说,因为前段无法发起该请求方式,所以需要配置过滤器进行拦截
org.springframework.web.filter.HiddenHttpMethodFilter
源码分析
HttpServletRequest requestToUse = request;
if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, paramValue);
}
}
filterChain.doFilter((ServletRequest)requestToUse, response);
源码分析
注意
如果tomcat的版本是8.0以上,则jsp页面只接受get和post请求,解决方法,在页面中加入 isErrorPage=“true” 即可