简体   繁体   English

Jersey客户端无法通过JAXB Moxy序列化发送/接收XML消息?

[英]Jersey Client can not send/receive XML messages with JAXB Moxy serialization?

I'm trying to use the jersey-client to make some RESTful requests with XML messages. 我正在尝试使用jersey-client用XML消息发出一些RESTful请求。 I don't want to serve any endpoints so there are no jersey-server packages involved. 我不想为任何端点提供服务,因此不涉及任何jersey-server软件包。
For the testing purposes I'm using the publicly reachable http://www.thomas-bayer.com/sqlrest/CUSTOMER testing service. 为了进行测试,我使用了可公开访问的http://www.thomas-bayer.com/sqlrest/CUSTOMER测试服务。

As stated in 9.2.4. 9.2.4所述。 Using Custom JAXBContext I have a custom ContextResolver class which is: 使用自定义JAXBContext我有一个自定义ContextResolver类,它是:

@Provider
@Produces({"application/xml"})
public class MyJaxbContextProvider implements ContextResolver<JAXBContext> {

  private JAXBContext context;

  public JAXBContext getContext(Class<?> type) {
    if (context == null) {
      try {
        context = JAXBContext.newInstance("resttest.jaxb");
      } catch (JAXBException e) {
        throw new RuntimeException(e);
      }
    }
    return context;
  }
}

This ContextResolver is registered in the rest client with: 该ContextResolver已在其他客户端中注册为:

client = ClientBuilder.newClient().register(MoxyXmlFeature.class).register(MyJaxbContextProvider.class);

My Customer entity is : 我的客户实体是:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "firstname",
    "lastname",
    "street",
    "city"
})
@XmlRootElement(name = "CUSTOMER")
public class Customer {
  @XmlElement(name = "ID")
  protected Integer id;

  @XmlElement(name = "FIRSTNAME")
  protected String firstName;

  @XmlElement(name = "LASTNAME")
  protected String lastName;

  @XmlElement(name = "STREET")
  protected String street;

  @XmlElement(name = "CITY")
  protected String city;

  // getters and setters following
  // ...
}

And finally the test class making the actual requests is: 最后发出实际请求的测试类是:

public class RestClientTest {
  private static Client client;

  @BeforeClass
  public static void beforeClass() {
    client = ClientBuilder.newClient().register(MoxyXmlFeature.class).register(MyJaxbContextProvider.class);
  }

  @Test
  public void testCreateCustomerWithEntity() { // Error
    Customer customer = new Customer();
    customer.setId(50);
    customer.setFirstName("Nikol");

    Response res = client.target("http://www.thomas-bayer.com/sqlrest/CUSTOMER/").request()
        .post(entity(customer, MediaType.APPLICATION_XML_TYPE));

  }

  @Test
  public void testGetCustomer() { // Error
    Customer customer = client.target("http://www.thomas-bayer.com/sqlrest/CUSTOMER/3/").request()
        .get(new GenericType<Customer>() {});

    assertThat(customer.getId(), equalTo(3));
  }
}

I have packed these files in a resttest project at https://github.com/georgeyanev/resttest 我已将这些文件打包在https://github.com/georgeyanev/resttest的Resttest项目中
After cloning the tests can be executed simply with 克隆后,可以使用

mvn test

I expect when I'm making a POST requests and passing a Customer instance the latter to be marshalled by the jersey client (testCreateCustomerWithEntity). 我希望在发出POST请求并传递Customer实例时,后者将由泽西岛客户端(testCreateCustomerWithEntity)编组。
And when I'm making a GET request the returned Customer entity to be unmarshalled (testGetCustomer). 当我进行GET请求时,返回的Customer实体将被解组(testGetCustomer)。
But both tests fail with MessageBodyProviderNotFoundException saying that there is no MessageBodyWriter/MessageBodyReader found for media type application/xml and type Customer. 但是,这两个测试均失败,并显示MessageBodyProviderNotFoundException,表示没有找到针对媒体类型application / xml和类型Customer的MessageBodyWriter / MessageBodyReader。

I'm using 2.19 version of both jersey-client and jersey-media-moxy libraries with oracle java 1.8.0_25 我正在使用带有Oracle Java 1.8.0_25的jersey-client和jersey-media-moxy库的2.19版本

What could be the possible reason for this? 这可能是什么原因?

It appears that an additional dependency for jersey-media-jaxb is needed in order for the custom ContextResolver to be picked by jersey. 似乎需要附加的jersey-media-jaxb依赖性,以便球衣可以选择自定义ContextResolver。 Then the standard JAXB mechanisms are used to define the JAXBContextFactory from which a JAXBContext instance would be obtained. 然后,使用标准的JAXB机制来定义JAXBContextFactory,从中可以获取JAXBContext实例。
In this case the JAXBContextFactory class is specified in jaxb.properties file in resttest.jaxb package. 在这种情况下,在resttest.jaxb包的jaxb.properties文件中指定JAXBContextFactory类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM