本文已收录在Github关注我,紧跟本系列专栏文章,咱们下篇再续!

  • 🚀 魔都架构师 | 全网30W技术追随者
  • 🔧 大厂分布式系统/数据中台实战专家
  • 🏆 主导交易系统百万级流量调优 & 车联网平台架构
  • 🧠 AIGC应用开发先行者 | 区块链落地实践者
  • 🌍 以技术驱动创新,我们的征途是改变世界!
  • 👉 实战干货:编程严选网

0 前言

@RequestParam、@PathVariable用于从request接收请求,都可接收参数:

  • @RequestParam从request里取值
  • @PathVariable从一个URI模板里来填充

1 @RequestParam

示例

URL如下:

http://localhost:8080/springmvc/hello/101?param1=java¶m2=edge

获取代码:

@RequestParam,@PathParam,@PathVariable等注解区别_spring

@RequestParam支持参数

package org.springframework.web.bind.annotation;

/**
 * 表明方法参数应绑定到Web请求参数。
 Spring MVC和Spring WebFlux中支持注释的处理程序方法:
 - Spring MVC中,“请求参数”映射到查询参数,形式数据和多部分请求中的部分。这是因为Servlet API将查询参数结合在一起,并将数据形成一个称为“参数”的单个地图,其中包括请求主体的自动解析。
 - Spring WebFlux中,“请求参数”映射仅查询参数。要使用所有3个,查询,形式数据和多部分数据,您可以使用数据绑定到用ModelAttribute注释的命令对象。

如果方法参数类型是Map ,并且指定了请求参数名称,则假设有适当的转换策略,请求参数值将转换为Map 。

如果方法参数为Map<String, String>或MultiValueMap<String, String> ,并且未指定参数名称,则映射参数将填充所有请求参数名称和值
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {

 /**
  * name属性别名
  */
 @AliasFor("name")
 String value() default "";

 /**
  * 绑定本次参数的名称,要跟URL上面的一样
  The name of the request parameter to bind to.
  * @since 4.2
  */
 @AliasFor("value")
 String name() default "";

 /**
  * 参数是否必须
  * <p>Defaults to {@code true}, leading to an exception being thrown
  * if the parameter is missing in the request. Switch this to
  * {@code false} if you prefer a {@code null} value if the parameter is
  * not present in the request.
  * <p>Alternatively, provide a {@link #defaultValue}, which implicitly
  * sets this flag to {@code false}.
  */
 boolean required() default true;

 /**
  * 如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值
  The default value to use as a fallback when the request parameter is
  * not provided or has an empty value.
  * <p>Supplying a default value implicitly sets {@link #required} to
  * {@code false}.
  */
 String defaultValue() default ValueConstants.DEFAULT_NONE;

}
/**
 * Common value constants shared between bind annotations.
 */
public interface ValueConstants {

    /**
     * Constant defining a value for no default - as a replacement for
     * {@code null} which we cannot use in annotation attributes.
     * <p>This is an artificial arrangement of 16 unicode characters,
     * with its sole purpose being to never match user-declared values.
     */
    String DEFAULT_NONE = "\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n";

}

2 @PathVariable

能识别URL里面的一个模板,看URL:

http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
1

这url可写成:

@RequestMapping("/hello/{id}")
public String getDetails(@PathVariable(value="id") String id,
@RequestParam(value="param1", required=true) String param1,
@RequestParam(value="param2", required=false) String param2){
.......
}

3 @PathParam

和spring的pathVariable一样基于模板,但是jboss包下的实现。

4 @QueryParam

JAX-RS 本来就提供的,和Spring的RequestParam作用一致。

5 @ResponseBody

表示服务器返回时的方式, 将内容或对象作为 HTTP 响应正文返回,值有很多,一般设定为json。

6 @RequestBody

一般post请求时才用该请求,把参数丢在requestbody。