0.SpringMVC中参数解析原理
1.入参出参类型限定
(此处以入参出参为json类型为例)2.入参是json类型,但是参数个数不确定
2.1方法一:
@PostMapping("/master")
public List<Stu> master(@RequestBody Map<String, Object> map){
System.out.println(map);
return stuService.hello();
}
postman访问,入参示例:打印的结果示例:
{first_name=Douglas, last_name=Fir, age=35, about=I like to build cabinets, interests=forestry}
2.2方法二:
@PostMapping("/master")
public List<Stu> master(@RequestBody String params){
Map<String, String> map = JSON.parseObject(params, Map.class);
Set<String> keys = map.keySet();
for (String key : keys){
System.out.println(key + "=============" + map.get(key));
}
return stuService.hello();
}
postman与上述2.1调用相同时,打印结果示例:
about=============I like to build cabinets
last_name=============Fir
interests=============forestry
first_name=============Douglas
age=============35
3.springMVC实现路径的模糊匹配
?: 匹配一个字符
* : 匹配任意字符
**: 匹配多层路径
3.1下述可以匹配的规则如:
http://localhost:8081/hello/tom/20/a/b/c/d/e/f
@RequestMapping(path = "/hello/{name}/{age}/**")
@ResponseBody
public Object hello(@PathVariable("name") String name,
@PathVariable("age") Integer age,
HttpServletRequest request){
return request.getRequestURL();
}
3.2下述可以匹配的url为:
http://localhost:8081/world/abc
@RequestMapping(path = "/world/{zzz:.*}")
@ResponseBody
public Object world(HttpServletRequest request){
return request.getRequestURL();
}
3.3采用代理模式修改RestTemplate
@AllArgsConstructor
public abstract class FilterRestTemplate implements RestOperations {
@Delegate // lombok的注解
protected volatile RestTemplate restTemplate;
}
4.常见问题
4.1编解码问题
服务端
前端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
<script type="text/javascript" src="../res/js/jquery-3.4.1.js"></script>
</head>
<body>
<div>
<form id="addAddress">
<input type="text" name="province" id="province">
<input type="text" name="addName" id="addName">
<input type="button" value="submit" id="submit"><br>
</form>
</div>
</body>
<script>
// 先引入 jquery
$("#submit").click(function () {
let str = {"province": $("#province").val(), "addName": $("#addName").val()};
$.ajax({
url: "http://localhost:8091/api/addAddress",
type: 'POST',
data: JSON.stringify(str),
contentType: "application/json",
success: function (msg) {
console.log("msg-->" + msg);
},
error: function (msg) {
console.log("msg-->" + msg);
}
})
})
</script>
</html>
https://www.jianshu.com/p/53b5bd0f1d44 (两种POST格式)
5.常用注解
package com.zy.spring.controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@RestController
public class HiController {
@RequestMapping("/pathVariable/{orderNo}/{orderMoney}")
public void pathVariable(@PathVariable("orderNo") String orderNo, @PathVariable("orderMoney") String orderMoney, HttpServletRequest request) {
System.out.println(orderNo);
System.out.println(orderMoney);
}
/**
* 表示这个参数是通过前端传递过来的,如果请求里没有这个参数,则会报错400 Bad Request。
* 这个注解用来解析请求路径里的参数(get请求)或者post请求中form表单格式的请求参数。
* 用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。
* @param orderNo
* @param request
*/
@RequestMapping("/requestParam")
public void requestParam(@RequestParam("orderNo") String orderNo, HttpServletRequest request) {
System.out.println(orderNo);
}
/**
* 用来接收POST请求BODY里的参数,格式为JSON格式。
* 用来处理Content-Type: 为 application/json、application/xml等格式的数据。
* @param maps
* @param request
*/
@RequestMapping("/requestBody")
public void requestBody(@RequestBody Map<String, String> maps, HttpServletRequest request) {
System.out.println(maps);
}
@RequestMapping("/requestHeader")
public void requestHeader(@RequestHeader Map<String, String> maps, HttpServletRequest request) {
System.out.println(maps);
}
/**
* @RequestAttribute 注解的参数在项目里是自己解析出来的,并不是前端传递的。
* 具体一点,在项目里的拦截器里会对Token信息进行解析,解析出来的参数重新放在请求里,
* (用httpServletRequest.setAttribute(name, value)),后边接口接收参数时就用这个注解。
* @param attr1
* @param request
*/
@RequestMapping("/requestAttribute")
public void requestAttribute(@RequestAttribute(value = "attr1") String attr1, HttpServletRequest request) {
System.out.println(attr1);
}
/**
* 在 header 中的 cookie 中设置
* @param authToken
* @param request
*/
@RequestMapping("/cookieValue")
public void cookieValue(@CookieValue(value = "authToken") String authToken, HttpServletRequest request) {
System.out.println(authToken);
}
}
参考资源
https://www.cnblogs.com/007tangtao/p/9251861.html?utm_source=debugrun&utm_medium=referral