2

I'm using jersey-client:2.28, jersey-hk2:2.28 and jersey-media-jaxb:2.28 maven targets to create a client that will consume a RESTful service. All beans have XmlRootElement annotations and default empty constructors. Yet when calling the service like this:

JAXBElement<XYZ> post = target.request( MediaType.APPLICATION_XML ).post( Entity.entity( xzyRequest, MediaType.APPLICATION_XML ), JAXBElement.class );

I get:

javax.ws.rs.client.ResponseProcessingException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/xml, type=class javax.xml.bind.JAXBElement, genericType=class javax.xml.bind.JAXBElement

I know the jersey-media-jaxb provider is being discovered, because it is being used to write the message body when initiating the request. I know the response has valid xml as I'm able to parse it like so:

String responseString = target.request( MediaType.APPLICATION_XML )
        .post( Entity.entity( xyzRequest, MediaType.APPLICATION_XML ), String.class );
Unmarshaller unmarshaller= JAXBContext.newInstance( XYZ.class ).createUnmarshaller();
XYZ zyx = ( ( JAXBElement<XYZ> ) unmarshaller.unmarshal( new StringReader( xmlResponse ) ) ).getValue();

I don't particularly mind parsing the XML, but I'd like to know why Jersey is unable to do so.

Alex Colomb
  • 170
  • 8
  • Is it the case where it works in the IDE but not with an uber jar, or is it just not working at all? And why are you using JAXBElement, and not just XYZ? – Paul Samsotha Feb 12 '19 at 03:47
  • Doesn't work in IDE or when deployed. With respect to unmarshalling to JAXBElement, see https://stackoverflow.com/questions/707084/class-cast-exception-when-trying-to-unmarshall-xml – Alex Colomb Feb 12 '19 at 04:08
  • Have you tried just using the Pojo? – Paul Samsotha Feb 12 '19 at 04:17
  • I'm facing a similar problem. Could you find a solution or at least a reason? – Esteve Aug 21 '20 at 10:22

0 Answers0