2013-08-23 46 views
2

我擴展了jersey-examples-moxy代碼以使用XML模式定義而不是JAXB帶註釋的bean。 xjc編譯的XML模式生成與原始示例相同的XML和JSON編碼。使用JAXBElement在Jersey 2.2下使用MOXy處理JSON的問題

我跟着球衣指令和所使用的的ObjectFactory以產生內CustomerResource.java的的JAXBElement客戶對象表示。我也如上所述修改了客戶端。我也納入PUT issues with JSON processing using JAXB under Jersey 2.2 with MOXy

MediaType.APPLICATION_XML功能完美描述的修補程序,並MediaType.APPLICATION_JSON作品入眼,而客戶未能馬歇爾JSON與「MessageBodyWriter未找到」一個PUT。以下異常被拋出:

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

謝謝你的幫助!

回答

2

你面臨的問題是在這條線:

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

基本上你傳遞JAXBElementEntity#json方法,但運行時沒有關於泛型類型的信息,你需要提供它。

webTarget 
    .request(MediaType.APPLICATION_JSON) 
    .put(Entity.json(new GenericEntity<JAXBElement<Customer>>(factory.createCustomer(customer)) {})); 
+0

這絕對解決了我的問題。 json的運行時沒有像xml運行時那樣處理JAXBElement有沒有特定的原因?任何你有機會參考這個地方的機會,所以我可以做一些進一步的閱讀,以避免差異的額外陷阱? – Hacksaw