jax rs - Problem with JAXB marshalling an object: javax.xml.bind.JAXBException: class ... or any of its super class is known to this context -
in jax-rs project (jersey) i'm having problem getting 1 of jaxb-annotated objects marshalled json. here error message see in logs:
severe: internal server error javax.ws.rs.webapplicationexception: javax.xml.bind.jaxbexception: class com.dnb.applications.webservice.mobile.view.companiesandlocations nor of super class known context.
does point specific problem? resource has method this:
@path("/name/{companyname}/location/{location}") @produces("application/json; charset=utf-8;") @consumes("application/json") @post public viewable findcompanybycompanynameandlocationasjson(@pathparam("companyname") string companyname, @pathparam("location") string location, criteriaview criteria) { criteria = criteria != null ? criteria : new criteriaview(); criteria.getkeywords().setcompanyname(companyname); return getcompanylistshandler().listsbycompanynameandlocation(criteria, location); }
viewable
empty interface. above method returns object of type companiesandlocations
, defined follows:
@xmlrootelement @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "companiesandlocations", proporder = { "count", "normalizedlocations", "companylist", "companymap", "modifiers", "modifiersmap", "companycount", "navigators" }) public class companiesandlocations extends basecompanies implements viewable { @xmlelement(name = "normalizedlocations", required = false) protected list<normalizedlocation> normalizedlocations; public list<normalizedlocation> getnormalizedlocations() { if (normalizedlocations == null) { normalizedlocations = new arraylist<normalizedlocation>(); } return normalizedlocations; } }
basecompanies defines number of other fields:
@xmltransient public abstract class basecompanies { @xmlelement(name = "modifiers", required = false) private list<modifiers> modifiers; ....
i should add i'm deviating approach used other working code in app. other resource methods objects objectfactory annotated @xmlregistry
. don't think necessary, though. have seen other code directly instantiates jaxb-annotated pojo'swithout using objectfactory factory.
any ideas?
jax-rs can bootstrap jaxbcontext, can't reach classes needs know about. reason can implement contextresolver. gives full control on how jaxbcontext created. below example of how might look:
package org.example.order; import javax.ws.rs.produces; import javax.ws.rs.ext.contextresolver; import javax.ws.rs.ext.provider; import javax.xml.bind.jaxbcontext; import javax.xml.transform.source; import javax.xml.transform.stream.streamsource; import org.eclipse.persistence.jaxb.jaxbcontextfactory; @provider @produces({"application/xml", "application/json"}) public class purchaseordercontextresolver implements contextresolver<jaxbcontext> { private jaxbcontext jaxbcontext; public purchaseordercontextresolver() { try { // bootstrap jaxbcontext necessary classes jaxbcontext = jaxbcontext.newinstance(purchaseorder.class); } catch(exception e) { throw new runtimeexception(e); } } public jaxbcontext getcontext(class<?> clazz) { if(purchaseorder.class == clazz) { return jaxbcontext; } return null; } }
Comments
Post a Comment