I extended the jersey-examples-moxy code to use an XML schema definition instead of the JAXB annotated beans. The xjc compiled XML schema produces XML and JSON encodings identical to the original example.
I followed the jersey instructions and used the ObjectFactory to generate the JAXBElement Customer object representation within CustomerResource.java. I also modified the client as described. I also incorporated the fix described in PUT issues with JSON processing using JAXB under Jersey 2.2 with MOXy
The MediaType.APPLICATION_XML functions perfectly, and MediaType.APPLICATION_JSON works for GETs, but the client is failing to marshall JSON on a PUT with "MessageBodyWriter not found". The following exception is thrown:
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)
Here is how I modified 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);
}
Here is how I am making the PUT request (same as for the functioning 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());
}
Thank you for your help!
The issue you're facing is on this line:
Basically you're passing
JAXBElement
toEntity#json
method but the runtime doesn't have information about the generic type, you need to provide it. That's what GenericEntity<T> class is for: