Skip to main content

Reading and Writing HTTP Messages

JAX-RS MessageBodyReader and MessageBodyWriter providers are used to deal with output and input parameters representing HTTP request and response messages, the same way it is done on the server side. Both WebClient and proxy clients can register custom readers and writers if needed, when dealing with custom formats or when the default providers shipped with CXF JAX-RS need to be configured.

For example, a WebClient or proxy call may return a javax.ws.rs.Response instance which usually implies that the client wishes to deal with the HTTP response directly: the status and headers are checked and then the response InputStream returned by default from Response.getEntity() is manually read; in fact, in case of proxies, Response is something a proxy needs to deal with whenever a current proxy method is typed to return Response. Often the Response.getEntity() InputStream can be read by the registered JAX-RS MessageBodyReader providers. This example shows how registering a custom CXF JAX-RS ResponseReader can help:

@Path("books")
public interface BookStore {
   @GET
   @Path("{id}")
   @Produces("application/xml")
   Response getBook(@PathParam("id") long id);
}

public class RESTClient {

   public void consumeRESTfulService(String baseAddress) {
      org.apache.cxf.jaxrs.client.ResponseReader responseReader =
         new org.apache.cxf.jaxrs.client.ResponseReader(Book.class);

      BookStore client = JAXRSClientFactory.create(
         baseAddress, BookStore.class);

      // get a single book
      Response response = client.getBook(1);
      Book book = response.readEntity(Book.class);
   }

}

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!