博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring之@PathVariable和@RequestParam
阅读量:4111 次
发布时间:2019-05-25

本文共 1370 字,大约阅读时间需要 4 分钟。

利用Spring注解MVC的时候我们发现有两个注解@PathVariable和@RequestParam

举例如下:

http://localhost:8080/api/show/id?id=22

在restful格式中我们采用

http://localhost:8080/api/show/{id}?id=22

这样path上的参数我们就可以通过@PathVariable获取,而参数上的值可以通过@RequestParam获取

针对其具体的属性我们查看源码:

public @interface PathVariable {	/** The URI template variable to bind to. */	String value() default "";}
public @interface RequestParam {	/**	 * The name of the request parameter to bind to.	 */	String value() default "";	/**	 * Whether the parameter is required.	 * 

Default is true, leading to an exception thrown in case * of the parameter missing in the request. Switch this to false * if you prefer a null in case of the parameter missing. *

Alternatively, provide a {@link #defaultValue() defaultValue}, * which implicitly sets this flag to false. */ boolean required() default true; /** * The default value to use as a fallback. Supplying a default value implicitly * sets {@link #required()} to false. */ String defaultValue() default ValueConstants.DEFAULT_NONE;}

我们发现@RequestParam有value和required两个属性

使用方式举例如下:

@RequestMapping(value = "/show/{id}", method = RequestMethod.GET)@ResponseBodypublic List
show(@PathVariable String id, @RequestParam(value = "id", required = false) String uid) { List
list = new ArrayList
(); for (int i = 0; i < 10; i++) { list.add(id + i); } return list;}

 

转载地址:http://njqsi.baihongyu.com/

你可能感兴趣的文章
mydata97的日期控件
查看>>
如何防止sql注入
查看>>
maven多工程构建与打包
查看>>
springmvc传值
查看>>
Java 集合学习一 HashSet
查看>>
在Eclipse中查看Android源码
查看>>
Android-Socket登录实例
查看>>
Android使用webservice客户端实例
查看>>
层在页面中的定位
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第四章 - 程序计数器
查看>>
第七章 - 本地方法栈
查看>>
第十一章 - 直接内存
查看>>