2

我扩展了jersey-examples-moxy代码以使用 XML 模式定义而不是 JAXB 注释 bean。xjc 编译的 XML 模式生成与原始示例相同的 XML 和 JSON 编码。

我按照球衣说明并使用ObjectFactory在 CustomerResource.java中生成JAXBElement Customer对象表示。我还按照描述修改了客户端。我还使用 JAXB 在 Jersey 2.2 下使用 MOXy 合并了 PUT 问题中描述的修复

MediaType.APPLICATION_XML完美运行,MediaType.APPLICATION_JSON适用于 GET,但客户端无法在 PUT 上编组 JSON,并显示“未找到 MessageBodyWriter”。抛出以下异常:

testJsonCustomer(org.glassfish.jersey.examples.jaxbmoxy.MoxyAppTest)  Time elapsed: 0.113 sec  <<< ERROR!
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class javax.xml.bind.JAXBElement, genericType=class javax.xml.bind.JAXBElement.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
at org.glassfish.jersey.filter.LoggingFilter.aroundWriteTo(LoggingFilter.java:268)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:290)

以下是我修改 CustomerResource.java 的方法:

private static ObjectFactory factory = new ObjectFactory();

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public JAXBElement<Customer> getCustomer() {
    return factory.createCustomer(customer);
}

@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public JAXBElement<Customer> setCustomer(Customer c) {
    customer = c;      
    return factory.createCustomer(customer);
}

这是我发出 PUT 请求的方式(与正常运行的 XML 相同):

@Override
protected void configureClient(ClientConfig clientConfig) {
    clientConfig.register(new MoxyXmlFeature());
}

@Test
public void testJsonCustomer() throws Exception {
    ObjectFactory factory = new ObjectFactory();
    final WebTarget webTarget = target().path("customer");

    // Target customer entity with GET and verify inital customer name.
    Customer customer = webTarget.request(MediaType.APPLICATION_JSON).get(Customer.class);
    assertEquals("Tom Dooley", customer.getPersonalInfo().getName());

    // Update customer name with PUT and verify operation successful.
    customer.getPersonalInfo().setName("Bobby Boogie");
    Response response = webTarget.request(MediaType.APPLICATION_JSON).put(Entity.json(factory.createCustomer(customer)));
    assertEquals(200, response.getStatus());

    // Target customer entity with GET and verify name updated.
    Customer updatedCustomer = webTarget.request(MediaType.APPLICATION_JSON).get(Customer.class);
    assertEquals(customer.getPersonalInfo().getName(), updatedCustomer.getPersonalInfo().getName());
}

谢谢您的帮助!

4

1 回答 1

2

您面临的问题是在这一行:

Response response = webTarget.request(MediaType.APPLICATION_JSON).put(Entity.json(factory.createCustomer(customer)));

基本上你正在传递JAXBElementEntity#json方法,但运行时没有关于泛型类型的信息,你需要提供它。这就是GenericEntity<T>类的用途:

webTarget
    .request(MediaType.APPLICATION_JSON)
    .put(Entity.json(new GenericEntity<JAXBElement<Customer>>(factory.createCustomer(customer)) {}));
于 2013-08-23T19:22:47.417 回答