Last active
December 20, 2015 22:18
-
-
Save theotherian/6203508 to your computer and use it in GitHub Desktop.
Jersey 2.0 client with generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It's easy to use Generics with Jersey 2.0 client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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