jaxbjerseyjax-rsjersey-2.0jersey-client

Support for XML in Jersey Client in a standalone application


I have a standalone application which connects to a RESTful API like this:

Client client = ClientBuilder.newClient();
WebTarget target = client.target(hostWithPort).path(apiConfig.getUrl());

CreateReq request = new CreateReq();//this is annotated with @XmlRootElement

CreateResult result = target
      .request(MediaType.TEXT_XML_TYPE)
      .post(Entity.entity(request, MediaType.TEXT_XML_TYPE), CreateResult.class);

The problem is that I get the following an MessageBodyProviderNotFoundException:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/xml, type=class com.mydomain.communication.CreateReq., genericType=class com.mydomain.communication.CreateReq.

According to the Jersey Documentation there must already be support for an JAXB Provider, but somehow it does no find it. I suppose I miss a dependency in my maven and some provider registration in my code. In my pom.xml I have only the dependency to org.glassfish.jersey.core -> jersey-client


Solution

  • Yeah I guess the jersey-client dependency doesn't pull in the required jersey-media-jaxb. Seems to be starting from 2.16 and up.

    As stated in the migration guide

    27.2. Migrating from Jersey 2.15 to 2.16

    27.2.1.1. JAX-B providers separated from the core

    From version 2.16 onwards, all JAX-B providers are being bundled in a separate module.