Close

JAX-RS - @DefaultValue examples

[Last Updated: Oct 29, 2025]

The annotation @DefaultValue can be used to define a default value for the following request annotations:

  • QueryParam
  • MatrixParam
  • CookieParam
  • FormParam
  • HeaderParam
  • PathParam

The value of the @DefaultValue is used when the corresponding meta-data is not present in the request.

In the following examples, we will quickly see how to use @Default annotation. In each example, we are using JAX-RS Client API to make request to the target resource.

@DefaultValue with @QueryParam

@Path("/")
public class ResourceExample {

    @GET
    @Path("test2")
    public String testQueryParam(
            @DefaultValue("default query param value")
            @QueryParam("queryParam1") String qpString) {
        return "query param value= " + qpString;
    }
}

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run
public class ClientUtil {

    public static WebTarget createRequest(String uri) {
        Client client = ClientBuilder.newClient();
        return client.target("http://localhost:8080" + uri);
    }
}
public class QueryParamClient {
    public static void main(String[] args) {

        WebTarget target = ClientUtil.createRequest("/test2");
        target = target.queryParam("queryParam1", 5);
        String response = target.request().get(String.class);
        System.out.printf("response with query param:%n%s%n", response);

        target = ClientUtil.createRequest("/test2");
        response = target.request().get(String.class);
        System.out.printf("response without query param:%n%s%n", response);
    }
}

Output

response with query param:
response from: http://localhost:8080/test2?queryParam1=5
PathNum: 2
query paramMyResource: null

response without query param:
response from: http://localhost:8080/test2
PathNum: 2
query paramMyResource: null

@DefaultValue with @MatrixParam

@Path("/")
public class ResourceExample {

    @GET
    @Path("test3")
    public String test3(
            @DefaultValue("default matrix param value")
            @MatrixParam("matrixParam1") String mpString) {
        return "matrix param value= " + mpString;
    }
}
public class MatrixParamClient {
    public static void main(String[] args) {

        WebTarget target = ClientUtil.createRequest("/test3");
        target = target.matrixParam("matrixParam1", 5);
        String response = target.request().get(String.class);
        System.out.printf("response with matrix param:%n%s%n", response);

        target = ClientUtil.createRequest("/test3");
        response = target.request().get(String.class);
        System.out.printf("response without matrix param:%n%s%n", response);
    }
}

Output

response with matrix param:
response from: http://localhost:8080/test3;matrixParam1=5
PathNum: 3
query paramMyResource: null

response without matrix param:
response from: http://localhost:8080/test3
PathNum: 3
query paramMyResource: null

@DefaultValue with @CookieParam

@Path("/")
public class ResourceExample {

    @GET
    @Path("test4")
    public String test4(
            @DefaultValue("default cookie param value")
            @CookieParam("cookieParam1") String cString) {
        return "cookie value= " + cString;
    }
}
public class CookieParamClient {
    public static void main(String[] args) {

        WebTarget target = ClientUtil.createRequest("/test4");

        String response = target.request()
                                .cookie("cookieParam1", "cookie param value")
                                .get(String.class);
        System.out.printf("response with cookie param:%n%s%n", response);

        //without cookie param
        target = ClientUtil.createRequest("/test4");
        response = target.request().get(String.class);
        System.out.printf("response without cookie param:%n%s%n", response);
    }
}

Output

response with cookie param:
response from: http://localhost:8080/test4
PathNum: 4
query paramMyResource: null

response without cookie param:
response from: http://localhost:8080/test4
PathNum: 4
query paramMyResource: null

@DefaultValue with @FormParam

@Path("/")
public class ResourceExample {

    @POST
    @Path("test5")
    public String test5(
            @DefaultValue("default form param value")
            @FormParam("formParam1") String fString) {
        return "FormParam value= " + fString;
    }
}
public class FormParamClient {
    public static void main(String[] args) {

        WebTarget target = ClientUtil.createRequest("/test5");

        Form form = new Form();
        form.param("formParam1", "form param test value");

        Entity<Form> entity = Entity.entity(form,
                MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        String response = target.request().post(entity, String.class);
        System.out.printf("response with form param:%n%s%n", response);

        target = ClientUtil.createRequest("/test5");
        form = new Form();
        entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
        response = target.request().post(entity, String.class);
        System.out.printf("response without form param:%n%s%n", response);
    }
}

Output

response with form param:
FormParam value= form param test value
response without form param:
FormParam value= default form param value

@DefaultValue with @HeaderParam

@Path("/")
public class ResourceExample {

    @GET
    @Path("test6")
    public String test6(
            @DefaultValue("default header param value")
            @HeaderParam("headerParam1") String hString) {
        return "HeaderParam value= " + hString;
    }
}
public class HeaderParamClient {
    public static void main(String[] args) {

        WebTarget target = ClientUtil.createRequest("/test6");
        String response = target.request()
                                .header("headerParam1", "header param value")
                                .get(String.class);
        System.out.printf("response with header param:%n%s%n", response);

        target = ClientUtil.createRequest("/test6");
        response = target.request().get(String.class);
        System.out.printf("response without header param:%n%s%n", response);
    }
}

Output

response with header param:
HeaderParam value= header param value
response without header param:
HeaderParam value= default header param value

@DefaultValue with @PathParam

using @DefaultValue with @PathParam cannot be very useful. That's because a part of the uri path cannot be null for a default value to be assigned.

In following example, Jersey implementation throws '404 Not Found' exception, when a path param is not being used on the client side.

@Path("/")
public class ResourceExample {

    @GET
    @Path("test/{path1}test1")
    public String testPathParam(@DefaultValue("default path value")
                       @PathParam("path1") String path) {
        return "path value= " + path;
    }
}
public class PathParamClient {
    public static void main(String[] args) {
        WebTarget target = ClientUtil.createRequest("/test/test1");
        String response = target.request().get(String.class);
        System.out.println(response);
    }
}

Output

javax.ws.rs.NotFoundException: HTTP 404 Not Found
at org.glassfish.jersey.client.JerseyInvocation.convertToException (JerseyInvocation.java:1020)
at org.glassfish.jersey.client.JerseyInvocation.translate (JerseyInvocation.java:819)
at org.glassfish.jersey.client.JerseyInvocation.access$700 (JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call (JerseyInvocation.java:701)
at org.glassfish.jersey.internal.Errors.process (Errors.java:315)
at org.glassfish.jersey.internal.Errors.process (Errors.java:297)
at org.glassfish.jersey.internal.Errors.process (Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope (RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke (JerseyInvocation.java:697)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method (JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get (JerseyInvocation.java:316)
at com.logicbig.example.PathParamClient.main (PathParamClient.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.AbstractExecJavaBase.executeMainMethod (AbstractExecJavaBase.java:402)
at org.codehaus.mojo.exec.AbstractExecJavaBase.doExecClassLoader (AbstractExecJavaBase.java:377)
at org.codehaus.mojo.exec.AbstractExecJavaBase.lambda$execute$0 (AbstractExecJavaBase.java:287)
at java.lang.Thread.run (Thread.java:748)

In the following example, we are using Regex. Skipping a path param on the client side will map to an empty path param:

@Path("/")
public class ResourceExample {

    @GET
    @Path("test/{path1:.*}test2")
    public String testPathParam2(@DefaultValue("default path value")
                                @PathParam("path1") String path) {
        return "path value= " + path;
    }
}
public class PathParamClient2 {
    public static void main(String[] args) {

        WebTarget target = ClientUtil.createRequest("/test/test2");
        String response = target.request().get(String.class);
        System.out.println(response);
    }
}

Output

path value= 

If the value of the @PathParam is not defined in the @Path, then it will always be assigned to a default value:

@Path("/")
public class ResourceExample {

    @GET
    @Path("test1")
    public String testPathParam3(@DefaultValue("10")
                        @PathParam("path1") BigDecimal bd) {
        return "path param Value= " + bd;
    }
}
public class PathParamClient3 {
    public static void main(String[] args) {
        WebTarget target = ClientUtil.createRequest("/test1");
        String response = target.request().get(String.class);
        System.out.println(response);
    }
}

Output

path param Value= 10

Example Project

Dependencies and Technologies Used:

  • jersey-container-servlet 2.25.1: Jersey core Servlet 3.x implementation.
  • JDK 1.8
  • Maven 3.3.9

Defaul Value Examples Select All Download
  • jaxrs-default-value-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ResourceExample.java

    See Also