jaxb - Issues with JSON processing using JAXBElement under Jersey 2.2 with MOXy -
i extended jersey-examples-moxy code use xml schema definition instead of jaxb annotated beans. xjc compiled xml schema produces xml , json encodings identical original example.
i followed jersey instructions , used objectfactory generate jaxbelement customer object representation within customerresource.java. modified client described. incorporated fix described in put issues json processing using jaxb under jersey 2.2 moxy
the mediatype.application_xml functions perfectly, , mediatype.application_json works gets, client failing marshall json on put "messagebodywriter not found". following exception thrown:
testjsoncustomer(org.glassfish.jersey.examples.jaxbmoxy.moxyapptest) time elapsed: 0.113 sec <<< error! org.glassfish.jersey.message.internal.messagebodyprovidernotfoundexception: messagebodywriter not found media type=application/json, type=class javax.xml.bind.jaxbelement, generictype=class javax.xml.bind.jaxbelement. @ org.glassfish.jersey.message.internal.writerinterceptorexecutor$terminalwriterinterceptor.aroundwriteto(writerinterceptorexecutor.java:191) @ org.glassfish.jersey.message.internal.writerinterceptorexecutor.proceed(writerinterceptorexecutor.java:139) @ org.glassfish.jersey.filter.loggingfilter.aroundwriteto(loggingfilter.java:268) @ org.glassfish.jersey.message.internal.writerinterceptorexecutor.proceed(writerinterceptorexecutor.java:139) @ org.glassfish.jersey.message.internal.messagebodyfactory.writeto(messagebodyfactory.java:1005) @ org.glassfish.jersey.client.clientrequest.writeentity(clientrequest.java:430) @ org.glassfish.jersey.client.httpurlconnector._apply(httpurlconnector.java:290)
here how 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 how making put request (same 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 , verify inital customer name. customer customer = webtarget.request(mediatype.application_json).get(customer.class); assertequals("tom dooley", customer.getpersonalinfo().getname()); // update customer name put , 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 , verify name updated. customer updatedcustomer = webtarget.request(mediatype.application_json).get(customer.class); assertequals(customer.getpersonalinfo().getname(), updatedcustomer.getpersonalinfo().getname()); }
thank help!
the issue you're facing on line:
response response = webtarget.request(mediatype.application_json).put(entity.json(factory.createcustomer(customer)));
basically you're passing jaxbelement
entity#json
method runtime doesn't have information generic type, need provide it. that's genericentity<t> class for:
webtarget .request(mediatype.application_json) .put(entity.json(new genericentity<jaxbelement<customer>>(factory.createcustomer(customer)) {}));
Comments
Post a Comment