Skip to content

Instantly share code, notes, and snippets.

@theotherian
Last active December 20, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theotherian/6203508 to your computer and use it in GitHub Desktop.
Save theotherian/6203508 to your computer and use it in GitHub Desktop.
Jersey 2.0 client with generics
It's easy to use Generics with Jersey 2.0 client
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.jackson.JacksonFeature;
public final class MessageClient {
private static final MessageClient INSTANCE = new MessageClient();
private final Client client;
private MessageClient() {
client = ClientBuilder.newClient();
client.register(JacksonFeature.class);
}
public static List<Message> get(String name) {
return INSTANCE.client.target("http://localhost:8080/myapp").path("messages/" + name)
.request(MediaType.APPLICATION_JSON)
// using an anonymous GenericType instance will match the entity to List<Message>!
.get(new GenericType<List<Message>>(){});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment