I just found that org.jboss.resteasy.client.ClientRequest
is deprecated, invalidating everything I could find on Google about how to use the RESTEasy
c
The 3.0 beta documentation here describes these deprecations like so:
Resteasy manual client API, interceptors, StringConverters, StringParamterConverters, and Async HTTP APIs have all been deprecated and will be removed possibly in a later release. There is now a JAX-RS 2.0 equivalent for each of these things.
This would imply that the preferred method will be to use the JAX-RS Client API described in this post
The RESTEasy documentation says we should close the client connection; that would be client.close()
in your example. But every example I can find does not do this. Will the client connection get closed automatically during garbage collection?
If we assume there is a JSON API at http://example.org/pizza/{id}.json
, (where 'id' is a pizza ID) which returns results such as
{
"name": "Hawaiian",
"toppings": ["tomato", "ham", "cheese", "pineapple"]
}
Building on the Invocation.Builder Javadocs, we can do something like this,
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import org.glassfish.jersey.jackson.JacksonFeature;
public class PizzaClient {
private Client client;
public PizzaClient() {
client = ClientBuilder.newClient();
// enable POJO mapping using Jackson - see
// https://jersey.java.net/documentation/latest/user-guide.html#json.jackson
client.register(JacksonFeature.class);
}
/** POJO which maps to JSON results using Jackson */
public static class Pizza {
private String name;
private String[] toppings;
public String getName() { return name; }
public String[] getToppings() { return toppings ; }
}
public Pizza getPizzaById(String id) {
String uri = String.format("http://example.org/pizza/%s.json", id)
Invocation.Builder bldr = client.target(uri).request("application/json");
return bldr.get(Pizza.class);
}
public static void main(String[] args) {
PizzaClient pc = new PizzaClient();
Pizza pizza = pc.getPizzaById("1");
System.out.println(pizza.getName() + ":");
for (String topping : pizza.getToppings()) {
System.out.println("\t" + topping);
}
}
}
(this is also assisted by this post although that uses the deprecated API).
Note also that you may need to register a special handler if you want to use Jackson to read POJOs (or, I think, using JAXB) as documented here
Update You actually only need the following Maven dependencies:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.3.1</version>
</dependency>
(In which case you're not using RestEasy at all -- the javax.ws.rs
JAXRS implementation comes from Jersey)
OR you can stick with JBoss:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.4.Final</version>
</dependency>
In which case you can just remove the JacksonFeature line in the above code, and the code uses the more liberal Apache licence.