<div dir="ltr" style="text-align: justify;" trbidi="on">In this article we will explain about the<b> JAX-RS</b>, you can use <i><b>@QueryParam</b></i> annotation to inject URI query parameter into Java method. for example,</p>
<p> <i><b>/employees/query?url=dineshonjava.com</b></i></p>
<p> In above URI pattern, query parameter is <i><b>&#8220;url=dineshonjava.com&#8221;</b></i>, and you can get the url value with <i><b>@QueryParam(&#8220;url&#8221;).</b></i><br />
<b>1. <i>@QueryParam</i> example</b></p>
<p> See a full example of using<b> @QueryParam in JAX-RS.</b></p>
<pre class="highlight" name="code">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("/employee")
public class EmployeeController {

@GET
@Path("/query")
public Response getEmployees(
@QueryParam("from") int from,
@QueryParam("to") int to,
@QueryParam("orderBy") List<;String>; orderBy) {

return Response
.status(200)
.entity("getEmployees is called, from : " + from + ", to : " + to
+ ", orderBy" + orderBy.toString()).build();

}

}
</pre>
<p>URI Pattern : <i><b>&#8220;http://localhost:8181/sdnext/doj/employee/query?from=2300&;to=3300&;orderBy=age&;orderBy=name&#8221;</b></i></p>
<p> <i><b>getEmployees is called, from : 2300, to : 3300, orderBy[age, name]</b></i></p>
<p> <b>2. Programmatic Query Parameter</b></p>
<p> Alternatively, you can get the query parameters grammatically, via &#8220;@Context UriInfo&#8221;. See equivalent version below :</p>
<pre class="highlight" name="code">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("/employee")
public class EmployeeController {

@GET
@Path("/uriinfo")
public Response getEmployees(@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("getEmployees is called, from : " + from + ", to : " + to
+ ", orderBy" + orderBy.toString()).build();

}

}
</pre>
<div align='center' id="ads-id"></div>
<p>URI Pattern : <i><b>&#8220;http://localhost:8181/sdnext/doj/employee/uriinfo?from=2300&;to=3300&;orderBy=age&;orderBy=name&#8221;</b></i></p>
<p> <b>getEmployees is called, from : 2300, to : 3300, orderBy[age, name]</b></p>
<p> <b>3. <i>@DefaultValue</i> example</b></p>
<p> <i><b>@DefaultValue </b></i>is good for optional parameter.</p>
<pre class="highlight" name="code">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("/employee")
public class EmployeeController{
 
 @GET
 @Path("/default")
 public Response getEmployee(
 @DefaultValue("1000") @QueryParam("from") int from,
 @DefaultValue("999")@QueryParam("to") int to,
 @DefaultValue("name") @QueryParam("orderBy") List<;String>; orderBy) {
 
 return Response
 .status(200)
 .entity("getEmployees is called, from : " + from + ", to : " + to
 + ", orderBy" + orderBy.toString()).build();
 
 }
 
}
</pre>
<p><i><b>URI Pattern : &#8220;http://localhost:8181/sdnext/doj/employee/default&#8221;</b></i><br />
<i><b><br />
</b></i> <i><b>getEmployees is called, from : 1000, to : 999, orderBy[name]</b></i></p>
<p><b>Download SourceCode</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/JAX-RS%20%40QueryParam%20example.zip?attredirects=0&;d=1" target="_blank">JAX-RS @QueryParam example.zip</a></b></p>
<p><i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank">Wikipedia for REST Web Service</a></b></i></p>
<p> ; </p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/2013/06/jax-rs-pathparam-example.html#.Ub36hpyWyql">Previous </a><;<; ; ; || <a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html">Index </a>||  ; >;>;<a href="https://dineshonjava.com/2013/06/jax-rs-matrixparam-example.html">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jax-rs-pathparam-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/jax-rs-matrixparam-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '422'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…