Spring Boot中各种请求处理记录
PathVariable -获取路径中的参数
这里的pv是指拿到所有的请求参数
@GetMapping("/car/{id}/owner/{username}")
public Map<String, Object> getCar(@PathVariable("id") Integer id,
@PathVariable("username") String username,
@PathVariable Map<String,String> pv){
HashMap<String, Object> res = new HashMap<>();
res.put("id", id);
res.put("username", username);
res.put("pv", pv);
return res;
}
RequestHeader - 获取request header内容
/**
*
* http://127.0.0.1:8080/car2/2/owner/zhangsan
*
* @return
* {
* "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
* "header": {
* "host": "127.0.0.1:8080",
* "connection": "keep-alive",
* "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
}
*}
*/
@GetMapping("/car2/{id}/owner/{username}")
public Map<String, Object> getCar1(@RequestHeader Map<String,String> header,
@RequestHeader("User-Agent") String ua){
HashMap<String, Object> res = new HashMap<>();
res.put("User-Agent", ua);
res.put("header", header);
return res;
}
RequestParam - 获取URL中动态传的参数-问号后边
/**
* 获取url中动态传的参数
* // http://127.0.0.1:8080/car3?a=1&b=2&c=3
*
* {
* "a": 1,
* "param": {
* "a": "1",
* "b": "2",
* "c": "3"
* }
* }
*
*/
@GetMapping("/car3")
public Map<String, Object> getCar3(@RequestParam("a") Integer a,
@RequestParam Map<String,String> param){
HashMap<String, Object> res = new HashMap<>();
res.put("a", a);
res.put("param",param );
return res;
}
URL转发场景 - URL跳转
/**
* 转发场景- 访问/go路径自动转发到/success
*
* // http://127.0.0.1:8080/go
*
* {
* "msg1": "设置成功",
* "code1": 403
* }
*/
@Controller
public class RequestController {
@GetMapping("/go")
public String goPage(HttpServletRequest request){
request.setAttribute("msg", "设置成功");
request.setAttribute("code", 403);
return "forward:/success";
}
@ResponseBody
@GetMapping("/success")
public Map Success(@RequestAttribute("msg") String msg,
@RequestAttribute("code") Integer code,
HttpServletRequest request){
Object msg1 = request.getAttribute("msg");
HashMap<Object, Object> res = new HashMap<>();
res.put("msg1", msg1);
res.put("code1", code);
return res;
}
}
矩阵变量- URL中一个变量多个值,传list
样例: /cars/sell;low=1;brand=a,b,c
@RestController
public class ParameterTestController {
/**
*矩阵变量
* /cars/sell;low=1;brand=a,b,c
* SpringBoot默认是禁用了矩阵变量的功能,需要手动开启
* 对于路径的处理是由UrlPathHelper进行解析的, removeSemicolonContent 支持矩阵变量
*/
@GetMapping("/cars/{path}")
public Map carsSell(@MatrixVariable("low") Integer low,
@MatrixVariable("brand") List<String> brand,
@PathVariable("path") String path){
HashMap<Object, Object> result = new HashMap<>();
result.put("low1", low);
result.put("brand1", brand);
result.put("path", path);
return result;
}
}
修改配置UrlPathHelper,启用SpringBoot矩阵变量功能
第一种写法: 实现WebMvcConfigurer 接口
@Configuration(proxyBeanMethods = false)
public class WebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
}
第二种写法:
@Configuration(proxyBeanMethods = false)
public class WebConfig {
@Bean
public WebMvcConfigurer webMvcConfigurer(){
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setRemoveSemicolonContent(false);
configurer.setUrlPathHelper(urlPathHelper);
}
};
}
}
Spring Boot中静态资源访问
静态资源目录
类路径下:called /static( or /public or /resources or /META-INF/resources)
启动项目测试访问静态资源http://127.0.0.1:8080/static.pnghttp://127.0.0.1:8080/public.pnghttp://127.0.0.1:8080/resources2.pnghttp://127.0.0.1:8080/resources.jpg