JAX-RS receives an object as a JAXB object and as a String

I have a JAX-RS web service (using knitwear) that takes a JAXB object as a request object. When we get an error, we want to register the original xml string that was sent to us. I am currently simply reconfiguring the JAXB object, but since there are several java enumerations in these classes, enumeration values ​​that are incorrectly written in the xml source string are lost, which is not acceptable for our purposes.

Does anyone know a way to get a query object both a string and a JABX object? I would rather not write my own MessageBodyReader, and I would prefer not to try and not get MessageBodyReader for JAXB, if possible. You can also use jersey specific classes. We are using version 1.0.x.

+4
source share
2 answers

Turns out it's not that hard with the JAX-RS API. Here is 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 xml as a string and as a JAXB object in just a few lines of code.

+2
source

As an idea, you can add a servlet filter for your web application that will intercept all requests and capture the payload in the context of the stream, where it can be retrieved later if necessary.

0
source

All Articles