English 中文(简体)
JAX-RS get entity as JAXB object and as String
原标题:

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.

最佳回答

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.

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签