2

jersey-examples-moxyコードを拡張して、JAXB アノテーション付き Bean の代わりに XML スキーマ定義を使用しました。xjc でコンパイルされた XML スキーマは、元の例と同じ XML および JSON エンコーディングを生成します。

jersey の指示に従い、ObjectFactoryを使用して、CustomerResource.java 内にJAXBElement Customerオブジェクト表現を生成しました。説明どおりにクライアントも変更しました。また、 Jersey 2.2 と MOXy で JAXB を使用した JSON 処理に関する PUT の問題で説明されている修正も組み込みました。

MediaType.APPLICATION_XMLは完全に機能し、MediaType.APPLICATION_JSONは GET に対して機能しますが、クライアントは「MessageBodyWriter が見つかりません」で PUT で JSON をマーシャリングできません。次の例外がスローされます。

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