查缺补漏!学习SpringBoot:通过一个请求到底可以获得多少参数

时间:2021-08-06
本文章向大家介绍查缺补漏!学习SpringBoot:通过一个请求到底可以获得多少参数,主要包括查缺补漏!学习SpringBoot:通过一个请求到底可以获得多少参数使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

@GetMapping("hello/{id}/{name}")
public String hello(@PathVariable("id") String id,@PathVariable("name") String name){
System.out.println(value);
System.out.println(name);
return "hello";
}
id = liuyu ,name = qwert


**2.不指定参数名**

http://127.0.0.1:8080/hello/liuyu/qwert

@GetMapping("hello/{id}/{name}")
public String hello(@PathVariable Map<String,String> map){
    System.out.println(map);
    return "hello";
}

map = {id=liuyu, name=qwert}

## @MatrixVariable注解

如果要使用该注解需要开启配置

@Component
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
    UrlPathHelper urlPathHelper=new UrlPathHelper();
    urlPathHelper.setRemoveSemicolonContent(false);
    configurer.setUrlPathHelper(urlPathHelper);
}

}


该注解主要用法有两种,示例如下

**指定参数名**

http://127.0.0.1:8080/hello/liu;value=123

@GetMapping("hello/{id}")
public String hello(@PathVariable(name = "id") String name,@MatrixVariable(name = "value") String value){
System.out.println(name);
System.out.println(value);
return "hello";
}
id = liu
value = 123


**不指定参数名**

http://127.0.0.1:8080/hello/liu;value=123;name=qwe

@GetMapping("hello/{id}")
public String hello(@PathVariable(name = "id") String name,@MatrixVariable Map<String,String> value){
    System.out.println(name);
    System.out.println(value);
    return "hello";
}
id = liu
value = {value=123, name=qwe}

## @RequestBody注解

post 请求,将请具体封装成实体bean

post请求体
{
"name":"liu",
"id":"123"
}

@PostMapping("hello")
public String hello(@RequestBody User user){
    System.out.println(user);
    return "hello";
}

user(id=123, name=liu)

## @RequestHeader注解

获取请求头的字段,同样的有获取单个请求头和获取全部请求头的两种用法。


# 最后

每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。

如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份**限量版的Java零基础宝典**能够对你有所帮助。

领取这份**Java零基础宝典**,**[只需要点击这里即可免费下载](https://gitee.com/vip204888/java-p7)**

![](https://upload-images.jianshu.io/upload_images/22932333-7f13b76538e16344?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/22932333-48d8fbce839f757d?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

原文地址:https://www.cnblogs.com/dhsfdhfhgufdu/p/15108817.html