SpringBoot学习之-通过一个请求可以获得多少参数

栏目: Java · 发布时间: 6年前

内容简介:之前在该注解主要用法有两种,示例如下如果我们想获取全部的get请求值,可以不明确指定RequestParam注解的value,并且使用map来接收参数。这样我们就可以得到全部的请求值。

之前在 SpringBoot源码解析-controller层参数的封装 中已经分析过springboot中controller层参数封装的原理,但是工作中毕竟不会一直有时间给你慢慢分析,有时候快速查询也是很必要的。所以今天就总结一下controller层哪些参数封装注解的含义,以及使用,方便以后快速查阅。

@RequestParam注解

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

1.指定参数名

http://127.0.0.1:8080/hello?id=123
    
    @GetMapping("hello")
    public String hello(@RequestParam("id") String id){
        log.info("[{}]",id);
        return "hello";
    }
    
    id = 123
复制代码

2.不指定参数名

http://127.0.0.1:8080/hello?id=123&name=liuyu
    
    @GetMapping("hello")
    public String hello(@RequestParam Map<String,String> value){
        System.out.println(value);
        return "hello";
    }
    value = {id=123, name=liuyu}
复制代码

如果我们想获取全部的get请求值,可以不明确指定RequestParam注解的value,并且使用map来接收参数。这样我们就可以得到全部的请求值。

@PathVariable注解

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

1.指定参数名

http://127.0.0.1:8080/hello/liuyu/qwert
    
    @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注解

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

@CookieValue注解

获取cookie中的键值对的值。

请求头添加 Cookie:value=liuyu
    @GetMapping("hello")
    public String hello(@CookieValue(name = "value") String user){
        System.out.println(user);
        return "hello";
    }
    user = liuyu
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Flash ActionScript 3.0从入门到精通

Flash ActionScript 3.0从入门到精通

章精设、胡登涛 / 清华大学出版社 / 2008-10-1 / 69.00元

Flash ActionScript 3.0的出现,不仅从形式上改变了ActionScript,而且从本质上改变了ActionScript,使ActionScript 3.0成为了真正的面向对象编程语言。 本书从最简单的编程知识出发,带领读者走进编程的大门,是一本不可多得的ActionScript 3.0入门书。本书在注重基础的同时,从更高的层次来介绍ActionScript 3.0的面向对......一起来看看 《Flash ActionScript 3.0从入门到精通》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具