In JAX-RS, you can use @QueryParam
annotation to inject URI query parameter into Java method. for example,
/users/query?url=mkyong.com
In above URI pattern, query parameter is “url=mkyong.com“, and you can get the url value with @QueryParam("url")
.
1. @QueryParam example
See a full example of using @QueryParam
in JAX-RS.
import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @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(); } }
URI Pattern : “users/query?from=100&to=200&orderBy=age&orderBy=name”
getUsers is called, from : 100, to : 200, orderBy[age, name]
Like it ?
@QueryParam will convert the query parameter “orderBy=age&orderBy=name” into
@QueryParam will convert the query parameter “orderBy=age&orderBy=name” into
java.util.List
automatically.2. Programmatic Query Parameter
Alternatively, you can get the query parameters grammatically, via “@Context UriInfo
“. See equivalent version below :
import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; @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(); } }
URI Pattern : “users/query?from=100&to=200&orderBy=age&orderBy=name”
getUsers is called, from : 100, to : 200, orderBy[age, name]
3. @DefaultValue example
@DefaultValue
is good for optional parameter.
import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @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(); } }
URI Pattern : “users/query”
getUsers is called, from : 1000, to : 999, orderBy[name]
Download Source Code
Download it – JAX-RS-QueryParam-Example.zip (6 KB)
相关推荐
[奥莱理] RESTful Java 开发 (Jax-RS 实现) (英文版) [奥莱理] RESTful Java with Jax-RS (E-Book) ☆ 图书概要:☆ Learn how to design and develop distributed web services in Java using RESTful ...
The RESTEasy workbook that follows provides step-by-step instructions for installing, configuring, and running several working JAX-RS examples, using the JBoss RESTEasy implementation of JAX-RS 2.0. ...
javax.xml.ws.Service 报错需要的包,亲测可以用,直接下载在ide里buildpath一下就可以,四个jar包 ,整合了其他的jar所以配置简单
JAX-RS-FileUpload-Jersey-Example
JAX-RS(JSR 311)指的是Java API for RESTful Web Services,Roy Fielding也参与了JAX-RS的制订,他在自己的博士论文中定义了REST。对于那些想要构建RESTful Web Services的开发者来说,JAX-RS给出了不同于JAX-WS...
使用到的外部jar包有(必须的部分,需要加到Web容器中) •neethi-3.0.2.jar •jsr311-api-1.1.1.jar •cxf-bundle-2.6.0.jar 使用到的外部jar包有(可选的部分,当且仅当作为一个独立的application运行时) ...
使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的Restful服务.docx使用Eclipse开发基于SpringBoot+JAX-RS的...
关于JAX-RS的注解详细,本人通过网络及实际应用收集的资料
jax-rs 2.1规范最终版,英文原版。jax-rs规范定义了java平台Rest调用风格的WebService接口。
restlet_jax-rs列子 可直接运行 在我的博客里有一些描述。希望能给大家提供点帮助
JAX-RS REST helloworld 项目源码 下载后引入eclipse jee 启动tomcat可直接访问 访问路径为http://localhost:8080/AndroidJAX-RS/jaxrs/helloworld/html
web service rest(jax-rs)整合完整的Jax-rs组件包,内含2个版本,分别是1.0和1.4版本。
JAX-RS+spring
RESTful Java with JAX-RS 2.0.pdf
jax-rs官方实例,对大家学习restful开发会有很大启发。
JAX-RS Rest RestLet项目源码 对初学者有帮助 实现最简单的 GET PUT DELETE UPDATE
自己下的官网的文档然后作了个CHM。。。不太会做做得不大好。。不过能看。。比HTML方便一点
赠送jar包:jackson-jaxrs-json-provider-2.7.8.jar; 赠送原API文档:jackson-jaxrs-json-provider-2.7.8-javadoc.jar; 赠送源代码:jackson-jaxrs-json-provider-2.7.8-sources.jar; 赠送Maven依赖信息文件:...