views:

648

answers:

2

I have a JAX-RS web service (using jersey) that accepts a JAXB object as the request entity. When we get an error, we want to log the original xml string that was sent to us. Currently, I am just re-marshalling the JAXB object, but since we have several java enums in those classes, enum values that are not spelled correctly in the original xml string are lost, which is not acceptable for our purposes.

Does anyone know a way to get the request entity as both a string and JABX object? I would prefer not to write a custom MessageBodyReader and I would prefer to not try and get the MessageBodyReader for the JAXB if possible. You are free to use jersey-specific classes as well. We are using version 1.0.x.

A: 

As an idea, you can add a servlet filter for your web application that would intercept all requests and capture payload into a thread context where it can be extracted from later on if needed.

Eugene Kuleshov
A: 

Turns out it isn't that hard to do this with the JAX-RS API. Here's what I did:

@Path("/transactions")
public class TestResource<X> {

 private Class<X> jaxbClass;

 @POST
 @Path("/{transaction-id}")
 @Consumes("application/xml")
 public Response processPost(@Context Providers providers, @Context HttpHeaders httpHeaders, @PathParam("transaction-id") final long transactionId,
   final String xmlString) throws WebApplicationException, IOException {

  MessageBodyReader<X> reader = providers.getMessageBodyReader(jaxbClass, null, null, MediaType.APPLICATION_XML_TYPE);
  InputStream entityStream = new ByteArrayInputStream(xmlString.getBytes());
  final X xmlObject = reader.readFrom(jaxbClass, null, null, MediaType.APPLICATION_XML_TYPE, httpHeaders.getRequestHeaders(), entityStream);

  //insert logic here

  return Response.ok().build();
 }
}

This will give you the xml as a string and as a JAXB object in just a few lines of code.

Matt Campbell