Java Reference
In-Depth Information
write JAX-RS services, you are using the specification's annotations to turn an HTTP invoc-
ation into a Java method call. The RESTEasy Client Proxy Framework flips this around to
instead use the annotations to turn a method call into an HTTP request.
You start off by writing a Java interface with methods annotated with JAX-RS annotations.
For example, let's define a RESTful client interface to the customer service application we
have talked about over and over again throughout this topic:
@Path ( "/customers" )
public
public interface
interface CustomerResource
CustomerResource {
@GET
@Produces ( "application/xml" )
@Path ( "{id}" )
public
public Customer getCustomer ( @PathParam ( "id" ) int
int id );
@POST
@Consumes ( "application/xml" )
public
public Response createCustomer ( Customer customer );
@PUT
@Consumes ( "application/xml" )
@Path ( "{id}" )
public
public void
void updateCustomer ( @PathParam ( "id" ) int
int id , Customer cust );
}
This interface looks exactly like the interface a JAX-RS service might implement. Through
RESTEasy, we can turn this interface into a Java object that can invoke HTTP requests. To
do this, we use the org.jboss.resteasy.client.jaxrs.ResteasyWebTarget interface:
Client client = ClientFactory . newClient ();
WebTarget target = client . target ( "http://example.com/base/uri" );
ResteasyWebTarget target = ( ResteasyWebTarget ) target ;
CustomerResource customerProxy = target . proxy ( CustomerResource . class );
If you are using RESTEasy as your JAX-RS implementation, all you have to do is typecast
an instance of WebTarget to ResteasyWebTarget . You can then invoke the ResteasyWe-
bTarget.proxy() method. This method returns an instance of the CustomerResource inter-
face that you can invoke on. Here's the proxy in use:
// Create a customer
Customer newCust = new
new Customer ();
newCust . setName ( "bill" );
Response response = customerProxy . createCustomer ( newCust );
Search WWH ::




Custom Search