SpringMVC--请求传参总结

1. SpringMVC--请求传参总结


1.1 @RequestParam

类似于这样的传参方式 ?参数1=value1&参数2=value2
http://localhost:8081/postInfo?password=123456&userName=root

在控制器的处理方法入参处使用 @RequestParam可以把请求参数传递给请求方法。


1.1.1 源码分析

@RequestParam 注解源码

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
   @AliasFor("name")
   String value() default "";
   @AliasFor("value")
   String name() default "";
   boolean required() default true;
   String defaultValue() default ValueConstants.DEFAULT_NONE;
}

@RequestParam注解可以接受的参数:

  • name —— 参数名(可以用这个属性取别名,默认值为参数名)。
  • value —— 参数名(可以用这个属性取别名,默认值为参数名)。
  • required —— 是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常。
  • defaultValue —— 默认值,当没有传递参数时使用该值。

name 和 value属性的作用是一样的


1.1.2 实例

请求一(不包含默认值):

    @GetMapping("/postInfo")
    public String hello(@RequestParam(value = "userName") String userName, @RequestParam(value = "password") String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:
在这里插入图片描述


请求二(请求包含默认值):

    @GetMapping("/postInfo")
    public String hello(@RequestParam(value = "userName", defaultValue = "root") String userName, @RequestParam(value = "password") String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:
在这里插入图片描述
小结:
加了默认值后,当前参数的required 默认为 false,即使设置为required = true,也不会生效


请求三(设置required = false):

    @GetMapping("/postInfo")
    public String hello(@RequestParam(value = "userName", defaultValue = "root") String userName, @RequestParam(value = "password", required = false) String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:
在这里插入图片描述


请求四(value属性取别名):

    @GetMapping("/postInfo")
    public String hello(@RequestParam(value = "name") String userName, @RequestParam(value = "pwd") String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:
在这里插入图片描述


请求五(name属性取别名):

    @GetMapping("/postInfo")
    public String hello(@RequestParam(name = "name") String userName, @RequestParam(name = "pwd") String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:
在这里插入图片描述


请求六(什么都不加,默认为参数名):

    @GetMapping("/postInfo")
    public String hello(@RequestParam String userName, @RequestParam String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:
在这里插入图片描述


1.2 @PathVariable

RestFul风格
@PathVariable 常用于 RestFul风格

类似于这样的传参方式 /value1/value2
http://localhost:8081/postInfo/root/123456

1.2.1 @PathVariable 源码

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
	@AliasFor("name")
	String value() default "";
	@AliasFor("value")
	String name() default "";
	boolean required() default true;
}
  • name —— 参数名(可以用这个属性取别名,默认值为参数名)。
  • value —— 参数名(可以用这个属性取别名,默认值为参数名)。
  • required —— 是否必须。默认为 true, 表示请求参数中必须包含对应的参数,若不存在,将抛出异常。

name 和 value属性的作用是一样的


1.2.2 实例(由于跟@RequestParam类似,这里就简单写了)


请求:

    @GetMapping("/postInfo/{name}/{pwd}")
    public String hello(@PathVariable(value = "name") String userName, @PathVariable(value = "pwd") String password) {
        return "hello: " + userName + " and password is : " + password;
    }

运行结果:

在这里插入图片描述



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeJiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值