Hi, CC-ing to users,
This is really a JAXB related question, you'd add @XmlSeeAlso to the
base class, that would do as a start
Cheers, Sergey
On 07/07/14 19:22, Udara Liyanage wrote:
Hi,
I have a REST API which sends JSON as response. I want to implement a
proper format for all the REST API calls. *MyResponse* is the custom class
I implemented for the response.
@XmlRootElement
class MyResponse {
private String message;
private int code;
private Object data;
// Geter and seters are removed to shorten the email
}
Below is the API method.
@GET
@Path("/cartridge/available/list")
@Produces("application/json")
@Consumes("application/json")
@AuthorizationAction("/permission/protected/manage/monitor/tenants")
public Response getAvailableCartridges() throws RestAPIException {
List<Cartridge> cartridges =
ServiceUtils.getAvailableCartridges(null, null, getConfigContext());
Response.ResponseBuilder rb = Response.ok();
MyResponse mr = new MyResponse();
mr.setMessage("sucessess");
mr.setCode(300);
Cartridge cart = new Cartridge();
cart.setName("test");
*mr.setData(cart);*
return
Response.ok().entity(mr).type(MediaType.APPLICATION_JSON).build();
}
However above will end in below error.
Please note that if I set some primitive types for "data", it works fine.
What should be done to resolve the issue?
*What are the best practices when implementing custom responses with
complex types. *
*Caused by: javax.xml.bind.JAXBException: class
org.apache.stratos.manager.dto.Cartridge nor any of its super class is
known to this context.*
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:246)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:261)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:653)
at
com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152)
at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:340)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593)
at
com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:324)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
at
com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315)
at
com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:172)
at
org.apache.cxf.jaxrs.provider.json.JSONProvider.marshal(JSONProvider.java:488)
at
org.apache.cxf.jaxrs.provider.json.JSONProvider.marshal(JSONProvider.java:558)
at
org.apache.cxf.jaxrs.provider.json.JSONProvider.writeTo(JSONProvider.java:378)
... 40 more
Caused by: javax.xml.bind.JAXBException: class
org.apache.stratos.manager.dto.Cartridge nor any of its super class is
known to this context.
at
com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:594)
at
com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:648)
... 50 more
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com