728x90 AdSpace

Saturday, 16 November 2013

JAX-RS @QueryParam with @DefaultValue

In this tutorial we are going to see how set  default value to query parameters in RESTful web service using @DefaultValue
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project.
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
add jar 
4. Create package called com.javatutorialscorner.jaxrs.queryparam under RESTful-WebService

5. Create Java class QueryParamService under com.javatutorialscorner.jaxrs.queryparam package

QueryParamService .java
package com.javatutorialscorner.jaxrs.queryparam;

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;

public class QueryParamService {

public Response getTutorial(
@DefaultValue("www.javatutorialscorner.com") @QueryParam("site") String site,
@DefaultValue("Web Service Tutorial") @QueryParam("tutorial") String tutorial,
@DefaultValue("1") @QueryParam("rank") int rank) {
return Response
.entity("Web Site : " + site + " Tutorial : " + tutorial
+ " Rank : " + rank).build();

In above class @QueryParam used to get the value of URL query parameter into Java Class. If there is no query param in URL then default value set to the corresponding fields using @DefaultValue annotation

6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">


The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

Now you can run the service and access the service by calling the following URL



you can call the service by using web browser or from client class.
Web browser call
Output with parameter


Output with default value
default param

Create Client
1.Create package called com.javatutorialscorner.jaxrs.client under RESTful-WebService project
2.Create Client Java Class RESTfulClient under  com.javatutorialscorner.jaxrs.client package
RESTfulClient .java

package com.javatutorialscorner.jaxrs.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RESTfulClient {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
String baseURI = "http://localhost:8080/RESTful-WebService";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(baseURI);
System.out.println("output with Query param");
.queryParam("site", "www.javatutorialscorner.com")
.queryParam("tutorial", "RESTful WebService")
.queryParam("rank", "1").get(ClientResponse.class)
System.out.println("output with out Query param");


Now you can run the client and see the following output in console
output with Query param

Web Site : www.javatutorialscorner.com  Tutorial : RESTful WebService Rank : 1

output with out Query param

Web Site : www.javatutorialscorner.com  Tutorial : Web Service Tutorial Rank : 1
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: JAX-RS @QueryParam with @DefaultValue Rating: 5 Reviewed By: Annamalai Thangaraj