In RESTful web services (JAX-RS)
In JAX-RS, you can use
/users/query?url=codenuclear.com
Here is the final
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>WebServicesJAXRS</groupId> <artifactId>WebServicesJAXRS</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>WebServicesJAXRS</name> <description>Restfull Webservices</description> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.19</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-servlet</artifactId> <version>1.19</version> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> |
Now add Jersey servlet to our deployment descriptor
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.codenuclear</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> |
-
@QueryParam example
See a full example of using
@QueryParam in JAX-RS. -
Programmatic Query Parameter
Alternatively, you can get the query parameters grammatically, via “
@Context UriInfo “. See equivalent version below : -
@DefaultValue example
@DefaultValue is good for optional parameter.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.codenuclear; import java.util.List; import javax.ws.rs.DefaultValue; 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 RestServices { @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(); } } |
Output
URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/query?from=100&to=200&orderBy=age&orderBy=name”

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.codenuclear; import java.util.List; import javax.ws.rs.DefaultValue; 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 RestServices { @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(); } } |
Output
URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/query?from=300&to=500&orderBy=age&orderBy=name”

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.codenuclear; import java.util.List; import javax.ws.rs.DefaultValue; 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 RestServices { @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(); } } |
Output
URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/query”

I simply must tell you that you have an excellent and unique website that I must say enjoyed reading.