JAX-RS @DefaultValue Example

In our previous example we have learnt how to get the values using @QueryParam Annotation, now we will learn the usage of @DefaultValue annotation with @QueryParam. @DefaultValue is used to set a default value to the query param even when the parameters are not passed.

Folder Structure

  1. Create a Dynamic Web Project Restful_Example and create a package for our src files “com.javainterviewpoint
  2. Place the required jar files under WEB-INF/Lib 

    jersey-bundle-1.18.jar
    asm-3.1.jar

  3. Create the Java classes DefaultValueExample.java under  com.javainterviewpoint folder.
  4. Place the web.xml  under the WEB-INF directory

@Context Example

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("/DefaultValueExample")
public class DefaultValueExample 
{
    @GET
    public Response getQueryParameters(
            @DefaultValue("param1")@QueryParam("parameter1")String parameter1,
            @DefaultValue("param2")@QueryParam("parameter2")String parameter2)
    {
        
        String output = "Parameter1 :"+parameter1+" Parameter2 : "+parameter2;
        return Response.status(200).entity(output).build();
    }
}

Hit on the URI :

http://localhost:8080/RESTful_Example/rest/DefaultValueExample

Output

Parameter1 :param1 Parameter2 : param2

When we pass the parameters like below then the default values will be replaced with the one which we pass

http://localhost:8080/RESTful_Example/rest/DefaultValueExample?parameter1=”Java”&parameter2=”Interview”

Output

Parameter1 :"Java" Parameter2 : "Interview"

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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
 <display-name>Restful Web Application</display-name>
 <servlet>
 <servlet-name>jersey-serlvet</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.javainterviewpoint</param-value>
 </init-param>
 
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>jersey-serlvet</servlet-name>
 <url-pattern>/rest/*</url-pattern>
 </servlet-mapping>
</web-app>

We register our jersey container “com.sun.jersey.spi.container.servlet.ServletContainer” in the servlet-class of our web.xml and we will mention the source files location as the value to the init param “com.sun.jersey.config.property.packages” so that the container will scan for annotations of the class files within that package.

Leave a Reply

Your email address will not be published. Required fields are marked *