Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:37:00+00:00 2026-05-31T11:37:00+00:00

I use Jersey with JAXB to convert my response to XML. I have several

  • 0

I use Jersey with JAXB to convert my response to XML. I have several methods that consume XML from the Request Body. I can specify the bean as a parameter to the method being called and JAXB will be used to automatically transform the request body data to that bean, or fail if it’s not compatible.

This works great for the majority of all my cases, and I have not had the need to write a MessageBodyReader or MessageBodyWriter to handle the transformation to and from. I have this one instance where I need the request body to be mapped to the bean, but I also need the original XML that was in the body of the request. I need to store that in the DB.

I’ve tried getting to the request body through the HttpContext, and can’t figure out how – I just see the header values and URI values. I tried through the HTTPServletContext as well ending in the same result.

Is there a way to capture the request body as it is before it’s transformed or after? Will I need to create a MessageBodyReader to handle this case? It seemed like those were used to map the contents to a bean, which I already have functioning, so I didn’t think that was the solution. But, perhaps in the MessageBodyReader I can put the request body into some variable and pass it along while still transforming the data to the beans. That still seems kind of more work than is needed. I would think I could get to the body through the request somehow.

Update:
I tried getting the body from ContainerRequest object using the getEntity method.

    String xmlString = request.getEntity(String.class);

And, that worked great. The request body is captured in that property and I can add it to the request properties to access later in the resource, etc. The problem is, and I think it’s because the entity is a stream, it clears out the body. So by the time the jaxb mapping to the beans happen, there is nothing to transform and I’m given a bad request response.

That didn’t work but I wanted to throw it out there as a possibility if someone could get it working better.

Here is what I ended up doing, and it would be nice to not have to do it this way. I’m basically marshalling what was unmarshalled, in my service class.

    final StringWriter st = new StringWriter();
try
{

    final JAXBContext jaxbContext = JAXBContext.newInstance(full.classpath.and.classname.of.root.bean);
    final Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(unmarshalled object, st);
} catch (final JAXBException je)
{
    // TODO Auto-generated catch block
    je.printStackTrace();
}
final String xml = st.toString();

So you can see – I basically take the object, the root bean that the body was unmarshalled to, and marshal it, giving the location of the annotated root bean to use in marhsalling.

It seems a bit redonkulous to have to marshal what was already unmarshalled, and if that is the response, maybe marshalled again on the way out. I would like a solution where I could grab a copy of the body before the unmarshalling happens. But in the meantime, this will have to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:37:01+00:00Added an answer on May 31, 2026 at 11:37 am

    I ran into the same scenario and ended with similar conclusions, except I do it the other way around by getting it as a String and then marshal manually to avoid unnecessary JAXB work, e.g.:

    String xmlString = request.getEntity(String.class);
    Reader sReader = new StringReader(xmlString);
    return (T)jaxbCtx.createUnmarshaller().unmarshal(sReader);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody tell exactly use of Response.seeOther(...) in jersey i am trying to use
I have a JAX-RS web service (using jersey) that accepts a JAXB object as
I use jersey-client to consume a REST service. I need both the requested Entity
I have an application that uses Hibernate/JPA, with Spring and Jersey. In my application
How can one get the full HTTP REST request body for a POST request
How can I use non-jersey resources with jersey resources with guice ? I want
I'm using Google Guice with Jersey (jax-rs). Following method throws an JAXB-Exception (JAXB can't
I have a JAX-RS service (I use Jersey), and now I have to do
I've found a useful article that explains how to make Jersey to use SLF4J
I am using Jax-RS and jersey to implement a web service, I can use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.