The Resteasy Client Framework is the mirror opposite of the JAX-RS server-side specification. Instead of using JAX-RS annotations to map an incoming request to your RESTFul Web Service method, the client framework builds an HTTP request that it uses to invoke on a remote RESTful Web Service. This remote service does not have to be a JAX-RS service and can be any web resource that accepts HTTP requests.
Resteasy has a client proxy framework that allows you to use JAX-RS annotations to invoke on a remote HTTP resource.
The way it works is that you write a Java interface and use JAX-RS annotations on methods and the interface. For example:
public interface SimpleClient
{
@GET
@Path("basic")
@ProduceMime("text/plain")
String getBasic();
@PUT
@Path("basic")
@ConsumeMime("text/plain")
void putBasic(String body);
@GET
@Path("queryParam")
@ProduceMime("text/plain")
String getQueryParam(@QueryParam("param")String param);
@GET
@Path("matrixParam")
@ProduceMime("text/plain")
String getMatrixParam(@MatrixParam("param")String param);
@GET
@Path("uriParam/{param}")
@ProduceMime("text/plain")
int getUriParam(@PathParam("param")int param);
}
Resteasy has a simple API based on Apache HttpClient. You generate a proxy then you can invoke methods on the proxy. The invoked method gets translated to an HTTP request based on how you annotated the method and posted to the server. Here's how you would set this up:
import org.resteasy.plugins.client.httpclient.ProxyFactory;
...
// this initialization only needs to be done once per VM
ResteasyProviderFactory.initializeInstance();
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
SimpleClient client = ProxyFactory.create(SimpleClient.class, "http://localhost:8081");
client.putBasic("hello world");
Please see the ProxyFactory javadoc for more options. For instance, you may want to fine tune the HttpClient configuration.
@CookieParam works the mirror opposite of its server-side counterpart and creates a cookie header to send to the server. You do not need to use @CookieParam if you allocate your own javax.ws.rs.core.Cookie object and pass it as a parameter to a client proxy method. The client framework understands that you are passing a cookie to the server so no extra metadata is needed.
The client framework can use the same providers available on the server. You must manually register them through the ResteasyProviderSingleton using the addMessageBodyReader() and addMessageBodyWriter() methods.
If you'd like to use this functionality from Spring, you should add the following code:
<bean id="resteasyClientInitializer"
class="org.jboss.resteasy.plugins.spring.ResteasyClientInitializer" />
<bean id="simpleClient" class="org.jboss.resteasy.client.ProxyFactory" factory-method="create">
<constructor-arg value="com.yourcompany.SimpleClient" />
<constructor-arg value="http://localhost:8081" />
</bean>You only need to add one ResteasyClientInitializer to your Spring context, and you can add as many ProxyFactory instances that you'd like.
There are no comments on this article