我扩展了 jersey-examples-moxy 代码,以使用XML模式定义而不是JAXB带注释的bean。 xjc编译的XML模式生成与原始示例相同的XML和JSON编码。
我遵循了泽西指令并使用 ObjectFactory 在CustomerResource.java中生成 JAXBElement Customer 对象表示。我也按照描述修改了客户端。我还整合了PUT issues with JSON processing using JAXB under Jersey 2.2 with MOXy
中描述的修复程序MediaType.APPLICATION_XML 功能完美, MediaType.APPLICATION_JSON 适用于GET,但客户端无法在PUT上用“未找到MessageBodyWriter”编组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());
}
感谢您的帮助!
答案 0 :(得分:2)
您面临的问题是:
Response response = webTarget.request(MediaType.APPLICATION_JSON).put(Entity.json(factory.createCustomer(customer)));
基本上,您将JAXBElement
传递给Entity#json
方法,但运行时没有关于泛型类型的信息,您需要提供它。这就是GenericEntity<T>类的用途:
webTarget
.request(MediaType.APPLICATION_JSON)
.put(Entity.json(new GenericEntity<JAXBElement<Customer>>(factory.createCustomer(customer)) {}));