This example is a slight modification from ex06_1 and shows two different concepts. First, the same JAX-RS resource method can process two different media types. Chapter 9 gives the example of a method that returns a JAXB annotated class instance that can be returned as either JSON or XML. We’ve implemented this in ex09_1 by slightly changing the CustomerResource.getCustomer() method:
src/main/java/com/restfully/shop/services/CustomerResource.java
@Path("/customers")
public class CustomerResource {
...
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Customer getCustomer(@PathParam("id") int id)
{
...
}
The JAXB provider that comes with RESTEasy can convert JAXB objects to JSON or XML. In this example, we have added the media type application/json to getCustomer()’s @Produces annotation. The JAX-RS runtime will process the Accept header and pick the appropriate media type of the response for getCustomer(). If the Accept header is application/xml, XML will be produced. If the Accept header is JSON, the Customer object will be outputted as JSON.
The second concept being highlighted here is that you can use the @Produces annotation to dispatch to different Java methods. To illustrate this, we’ve added the getCustomerString() method, which processes the same URL as getCustomer() but for a different media type:
@GET
@Path("{id}")
@Produces("text/plain")
public Customer getCustomerString(@PathParam("id") int id)
{
return getCustomer(id).toString();
}
The client code for this example executes various HTTP GET requests to retrieve different representations of a Customer. Each request sets the Accept header a little differently so that it can obtain a different representation. For example:
src/test/java/com/restfully/shop/test/CustomerResourceTest.java
public class CustomerResourceTest
{
@Test
public void testCustomerResource() throws Exception
{
... initialization code ...
System.out.println("*** GET XML Created Customer **");
String xml = client.target(location).request()
.accept(MediaType.APPLICATION_XML_TYPE)
.get(String.class);
System.out.println(xml);
System.out.println("*** GET JSON Created Customer **");
String json = client.target(location).request()
.accept(MediaType.APPLICATION_JSON_TYPE)
.get(String.class);
System.out.println(json);
}
}
The SyncInvoker.accept() method is used to initialize the Accept header. The client extracts a String from the HTTP response so it can show you the request XML or JSON.
Perform the following steps: