在独立应用程序中支持 Jersey Client 中的 XML

Support for XML in Jersey Client in a standalone application

我有一个连接到 RESTful API 的独立应用程序,如下所示:

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);

问题是我得到以下 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.

根据 Jersey Documentation 必须已经支持 JAXB 提供程序,但不知何故它没有找到它。我想我错过了我的 Maven 中的依赖项和我的代码中的一些提供者注册。在我的 pom.xml 中,我只依赖于 org.glassfish.jersey.core -> jersey-client

是的,我猜是 jersey-client dependency doesn't pull in the required jersey-media-jaxb。似乎从 2.16 开始。

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.