JAX-RS之@QueryParam和@DefaultValue
来源:互联网 发布:淘宝开店保证金怎么退 编辑:程序博客网 时间:2024/06/06 15:40
先来看@queryparam
先看例子:
URL输入为:users/query?from=100&to=200&orderBy=age&orderBy=name
此时,输出为:
getUsers is called, from : 100, to : 200, orderBy[age, name]
要注意的是,跟@pathparam不同,@queryparam
中,指定的是URL中的参数是以键值对的形式出现的,而在程序中
@QueryParam("from") int from则读出URL中from的值,
而@pathparem中,URL中只出现参数的值,不出现键值对,比如:
“/users/2011/06/30”
则:
输出为:
getUserHistory is called, year/month/day : 2011/6/30
2 以动态的方式获得:
URL;users/query?from=100&to=200&orderBy=age&orderBy=name
输出为:
getUsers is called, from : 100, to : 200, orderBy[age, name]
注意这里把orderby后的两个参数读入为LIST处理了.
3 @DefaultValue,默认值
例子:
URL:users/query
输出:getUsers is called, from : 1000, to : 999, orderBy[name]
很好理解吧
先看例子:
- Path("/users")
- public class UserService {
- @GET
- @Path("/query")
- public Response getUsers(
- @QueryParam("from") int from,
- @QueryParam("to") int to,
- @QueryParam("orderBy") List<String> orderBy) {
- return Response
- .status(200)
- .entity("getUsers is called, from : " + from + ", to : " + to
- + ", orderBy" + orderBy.toString()).build();
- }
- }
Path("/users")public class UserService { @GET@Path("/query")public Response getUsers(@QueryParam("from") int from,@QueryParam("to") int to,@QueryParam("orderBy") List<String> orderBy) { return Response .status(200) .entity("getUsers is called, from : " + from + ", to : " + to+ ", orderBy" + orderBy.toString()).build(); } }
URL输入为:users/query?from=100&to=200&orderBy=age&orderBy=name
此时,输出为:
getUsers is called, from : 100, to : 200, orderBy[age, name]
要注意的是,跟@pathparam不同,@queryparam
中,指定的是URL中的参数是以键值对的形式出现的,而在程序中
@QueryParam("from") int from则读出URL中from的值,
而@pathparem中,URL中只出现参数的值,不出现键值对,比如:
“/users/2011/06/30”
则:
- @GET
- @Path("{year}/{month}/{day}")
- public Response getUserHistory(
- @PathParam("year") int year,
- @PathParam("month") int month,
- @PathParam("day") int day) {
- String date = year + "/" + month + "/" + day;
- return Response.status(200)
- .entity("getUserHistory is called, year/month/day : " + date)
- .build();
- }
@GET@Path("{year}/{month}/{day}")public Response getUserHistory(@PathParam("year") int year,@PathParam("month") int month, @PathParam("day") int day) { String date = year + "/" + month + "/" + day; return Response.status(200).entity("getUserHistory is called, year/month/day : " + date).build(); }
输出为:
getUserHistory is called, year/month/day : 2011/6/30
2 以动态的方式获得:
- @Path("/users")
- public class UserService {
- @GET
- @Path("/query")
- public Response getUsers(@Context UriInfo info) {
- String from = info.getQueryParameters().getFirst("from");
- String to = info.getQueryParameters().getFirst("to");
- List<String> orderBy = info.getQueryParameters().get("orderBy");
- return Response
- .status(200)
- .entity("getUsers is called, from : " + from + ", to : " + to
- + ", orderBy" + orderBy.toString()).build();
- }
@Path("/users")public class UserService { @GET@Path("/query")public Response getUsers(@Context UriInfo info) { String from = info.getQueryParameters().getFirst("from");String to = info.getQueryParameters().getFirst("to");List<String> orderBy = info.getQueryParameters().get("orderBy"); return Response .status(200) .entity("getUsers is called, from : " + from + ", to : " + to+ ", orderBy" + orderBy.toString()).build(); }
URL;users/query?from=100&to=200&orderBy=age&orderBy=name
输出为:
getUsers is called, from : 100, to : 200, orderBy[age, name]
注意这里把orderby后的两个参数读入为LIST处理了.
3 @DefaultValue,默认值
例子:
- @Path("/users")
- public class UserService {
- @GET
- @Path("/query")
- public Response getUsers(
- @DefaultValue("1000") @QueryParam("from") int from,
- @DefaultValue("999")@QueryParam("to") int to,
- @DefaultValue("name") @QueryParam("orderBy") List<String> orderBy) {
- return Response
- .status(200)
- .entity("getUsers is called, from : " + from + ", to : " + to
- + ", orderBy" + orderBy.toString()).build();
- }
@Path("/users")public class UserService { @GET@Path("/query")public Response getUsers(@DefaultValue("1000") @QueryParam("from") int from,@DefaultValue("999")@QueryParam("to") int to,@DefaultValue("name") @QueryParam("orderBy") List<String> orderBy) { return Response .status(200) .entity("getUsers is called, from : " + from + ", to : " + to+ ", orderBy" + orderBy.toString()).build(); }
URL:users/query
输出:getUsers is called, from : 1000, to : 999, orderBy[name]
很好理解吧
- JAX-RS之@QueryParam和@DefaultValue
- JAX-RS @QueryParam example
- @queryparam 与 @DefaultValue("name")
- JAX-RS之@matrixParam和@PathParam
- JAX-RS之@formparam和@HeaderParam
- WebService JAX-WS和JAX-RS
- Java JAX-WS和JAX-RS webservice
- Java JAX-WS和JAX-RS webservice
- JAX-RS之上传文件
- JAX-RS之Jersey入门
- JAX-RS和spring整合
- WebService CXF-RS技术之@Pathparam与@Queryparam注解区别
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- JAX-RS
- vc修改列表框LISTBOX的背景颜色及字体颜色及其使用方法
- 读C++ Primer 之队列类模板
- Linux下常见文件解压方法及命令
- JAX-RS之@matrixParam和@PathParam
- yaffs格式根文件系统制作
- JAX-RS之@QueryParam和@DefaultValue
- linux中apt-cache命令的用法
- JAX-RS之@formparam和@HeaderParam
- JAX-RS之下载文件
- Android学习-计算器1
- 进程地址空间地址转换为物理地址
- 免费的Bug管理系统----BugFree的安装与使用
- java ResultSet获得总行数、总列数的用法详解
- JAX-RS之上传文件