21

I have a RESTful API who's document says that a certain query parameter is optional, and does not supply a default argument. So, I can either supply the value or not send it in the GET request as a parameter.

Example:

  • queryA is required
  • queryB is optional (can send GET without it)

This should work:

http://www.example.com/service/endpoint?queryA=foo&queryB=bar

This should also work:

http://www.example.com/service/endpoint?queryA=foo

How do I make an client interface for Jersey-Proxy that can do this?? I do not have the server-side code to interface with so I am using org.glassfish.jersey.client.proxy.WebResourceFactory via Jersey-Proxy to generate the client to interact with the server API.

Sample interface:

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/service")
@Produces("application/json")
public interface ServiceInterface {

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first,
            @QueryParam("queryB") String second);

}

I know I can make another method:

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first);

But what happens when you have multiple optional fields?? I don't want to make every possible mutation of them!

2

2 Answers 2

36

The interface was right all along

I can't believe it was this easy:

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/service")
@Produces("application/json")
public interface ServiceInterface {

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first,
            @QueryParam("queryB") String second);

}

Notice anything different than the questions interface?? Nope. That's because that is the answer!


Don't use @DefaultValue for optional parameters

If you want to default a parameter to a specific value, you use the @DefaultValue annotation in the parameter:

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/service")
@Produces("application/json")
public interface ServiceInterface {

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first,
            @QueryParam("queryB") @DefaultValue("default") String second);

}

Pass null to the @QueryParam you don't want

If you want to make the @QueryParam optional, you do not apply the @DefaultValue annotation. To pass a value with the query parameter, just pass in the value normally. If you would like the query parameter to not show up at all, just pass null!

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/service")
@Produces("application/json")
public interface ServiceInterface {

    @Path("/endpoint")
    @GET
    public Response getEndpoint(
            @QueryParam("queryA") String first,
            // Pass null to this parameter to not put it in the GET request
            @QueryParam("queryB") String second);

}

So calling ServiceInterface.getEndpoint("firstQueryParam", "secondQueryParam"); calls:

http://targethost.com/service/endpoint?queryA=firstQueryParam&queryB=secondQueryParam

and calling ServiceInterface.getEndpoint("firstQueryParam", null); calls:

http://targethost.com/service/endpoint?queryA=firstQueryParam

And walla! No second query parameter! :)

Note on primitive values

If your API takes primitive values (like int, float, boolean, etc), then use the object wrapper class (Autoboxing) for that primitive (like Integer, Float, Boolean, etc). Then, you can pass null to the method:

public Response getEndpoint(@QueryParam("queryA") Boolean first);
3
  • 1
    what about numbers in stead of strings? There is no way of using null, so you have to check value.
    – jlanza
    Jul 29, 2016 at 10:58
  • I believe to do that you need to use the auto-boxes types: Boolean, Integer, etc....
    – justderb
    Aug 2, 2016 at 15:45
  • Jersey is very annoying if you need to maintain multiple client and they can't all upgrade to your new API, basically it forces you to introduce a breaking change in your interface for an optional query param Sep 9, 2019 at 17:18
3

You can inject a UriInfo instance (or something else like HttpServletRequest) into your method, and get whatever data you want off of it.

For example

@Path("/endpoint")
@GET
public Response getEndpoint(@Context UriInfo info, @QueryParam("queryA") String queryA) {
  String queryB = info.getQueryParameters().getFirst("queryB");
  if (null != queryB) {
    // do something with it
  }
  ...
}
4
  • Does this work with WebResourceFactory with Jersey off the top of your head? I'll look into seeing if I can do this...
    – justderb
    May 10, 2015 at 2:35
  • You mention Jersey client, but as far as I can tell your example is the server-side definition of a JAX-RS endpoint...
    – Alex
    May 10, 2015 at 2:37
  • The definition is for client-side, as I do not have the server-side code.
    – justderb
    May 10, 2015 at 2:38
  • Ok I understand, I'm not sure if you can use the @Context style injection then... but it may work.
    – Alex
    May 10, 2015 at 2:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.