Spring Boot 前后端参数交互方式——注解

@RequestBody——获取请求参数中的json字符串

很常用,推荐链接

@RequestParam——获取传入参数的值

作用: 指定参数名获取传入参数的值,如果方法参数是Map<String、String>、MultiValueMap<String、String>或HttpHeaders,则映射将填充所有头部名称和值。
参数:

	//同name()方法
  	 @AliasFor("name")
    String value() default "";
    //绑定到的请求参数的名称。
    @AliasFor("value") 
    String name() default "";
	//参数是否必须  默认是true,如果请求不存在该参数,则抛出异常;如果为false,如果请求不存在该参数,形参为null ,
    boolean required() default true;

    String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";

例子

@RequestMapping(value = "/test")
public Object test(@RequestParam(value = "name",required = false) String name
,@RequestParam(value = "id") String id) {
        return id+"-----"+name;
    }
//get   /test?name=sjy&id=1    输出  1-----sjy
//get   /test?id=1			 输出  1-----null
//get   /test?name=sjy         输出 400 – 错误的请求

@PathVariable—— 获取定义在路径中的参数值

作用: 用来映射URL中的占位符到目标方法的参数中
参数:

 //要绑定到的路径变量的名称。
 String value() default ""; 

例:

@RequestMapping(value = "/test1/{id}/test1/{name}")
public Object test1(@PathVariable(value = "id") String id,
    @PathVariable(value = "name") String name) {
        return id+"-----"+name;
    }
//请求   /test1/11/test1/211    输出   11-----211

@MatrixVariable——使用矩阵变量绑定参数

作用: 使用矩阵变量绑定参数,Spring4.0已经全面支持Matrix Variable,该注解似的开发人员能够将请求中的矩阵变量绑定到处理器的方法参数中。
Matrix Variable中,多个变量可以使用”;“(分号)分隔,
提示: 默认Spring MVC中Matrix Variable功能是关闭的,如果要开启该功能,需要在spring-mvc配置文件中添加如下设置 <mvc:annotation-driven enable-matrix-variables=“true”/> 。

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
           <!--开启Matrix Variable功能-->
    <mvc:annotation-driven enable-matrix-variables="true"/>
    <context:component-scan base-package="com.sjy"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/views/"
          p:suffix=".jsp"/>
</beans>

如果在spring boot 想要使用该功能需要如下配置,重写WebMvcConfigurer接口的configurePathMatch方法即可

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

例如: /test2;id=1;name=sjy
参数:

	// 同name()方法
	@AliasFor("name") 
    String value() default "";
	// 矩阵变量的名称。如果方法参数是Map<String、String>、MultiValueMap<String、String>或HttpHeaders,则映射将填充所有头部名称和值。
    @AliasFor("value")
    String name() default "";
	//如果需要消除歧义,矩阵变量所在的URI路径变量的名称 看下面例子理解
    String pathVar() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
	//是否需要矩阵变量。 
    boolean required() default true;

例子:

@RequestMapping(value="test2/{id}/p/{name}")
public Object test2(@MatrixVariable(pathVar = "id") Map<String, String[]> id,
@MatrixVariable(pathVar = "name") Map<String, String[]> name){
    return id+"-----"+name;
    }
    
//请求  /test2/1s;a=1,2,3;b=a;c=cc/p/sjy;s=2;j=3;y=3;d=a,b,c,d   
//输出响应  {a=[1, 2, 3], b=[a], c=[cc]}-----{s=[2], j=[3], y=[3], d=[a, b, c, d]}
//如果将@MatrixVariable注解中的pathVar属性去掉
//相同的请求
//输出响应  {a=[1, 2, 3], b=[a], c=[cc], s=[2], j=[3], y=[3], d=[a, b, c, d]}-----{a=[1, 2, 3], b=[a], c=[cc], s=[2], j=[3], y=[3], d=[a, b, c, d]}

//完整性测试
@RequestMapping(value = "/test3/{ownerId}/test3/{petId}", method = RequestMethod.GET)
    public Object findPet(
    @PathVariable(value = "ownerId") Integer ownerId ,
    @PathVariable(value = "petId") Integer petId ,
    @MatrixVariable(value = "q", pathVar = "ownerId") String q,
    @MatrixVariable(pathVar = "ownerId") Map<String, String> matrixVars,
    @MatrixVariable(pathVar = "petId") Map<String, String> petMatrixVars) {
      return "ownerId: " + ownerId + "----petId:" + petId + "----q: " + q + 
                "----matrixVars: " + matrixVars + "---petMatrixVars: " + petMatrixVars;
    }
	// 请求   /test3/1;q=1,2,3;b=a;c=cc/test3/12;s=2;j=3;y=3;d=a,b,c,d
    // 输出响应 ownerId: 1----petId: 12----q: 1,2,3----matrixVars: {q=[1, 2, 3], b=[a], c=[cc]}---petMatrixVars: {s=[2], j=[3], y=[3], d=[a, b, c, d]}

@RequestHeader——请求报文头的值

**作用:**指定报文头参数名获取传入参数的值,如果方法参数是Map<String、String>、MultiValueMap<String、String>或HttpHeaders,则映射将填充所有头部名称和值。
参数:

 	 @AliasFor("name")
    String value() default "";
	//要绑定到的请求头的名称
    @AliasFor("value")
    String name() default "";
	//是否需要该参数
    boolean required() default true;

例: 使用PostMan

@RequestMapping("/test4")
    public Object test4(@RequestHeader Map<String, String> map){
        return map.toString();
    }

在这里插入图片描述

@CookieValue——请求中Cookie的值

与@RequestHeader同理,不过是获取的Cookie中的值。


如有不当之处,欢迎指正

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值