You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sudheer-3 <su...@gmail.com> on 2010/08/10 13:21:38 UTC

JAXB only converting the base class not the derived class

We have implemented RESTFul service using Apache CXF. Used JSON as the
communication between our GWT client and RESTFul server.

Our DTOs are having structure Similar to below. Every DTO should extend
BaseDTO.

@XmlRootElement(name = "BaseDTO")
public class BaseDTO implements Serializable {

    private String UUID;
    public String getUUID() {
        return UUID;
    }
    public void setUUID(String UUID) {
        this.UUID = UUID;
    }
}

@XmlRootElement(name = "Agent")
public class Agent extends BaseDTO {

    private String description;
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

We have configured JSONProvider in spring-config.xml as below

   <jaxrs:server id="restServer" address="/services/">
        <jaxrs:serviceBeans>
            <ref bean="agentService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="feed" value="application/atom+xml"/>
            <entry key="json" value="application/json"/>
            <entry key="xml" value="application/xml"/>
            <entry key="html" value="text/html"/>
        </jaxrs:extensionMappings>
        
        <jaxrs:providers>
            <bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
                <property name="writeXsiType" value="false" />
                <property name="readXsiType" value="false" />
                <property name="jaxbElementClassMap" value="" />
            </bean>
        </jaxrs:providers>
    </jaxrs:server>


Methods from AgentService.java returns List<Agent> and we put this in a
common response format defined below and constructs response using Response
(javax.ws.rs.core.Response.ok(UIServiceResponse<T>)).

@XmlRootElement(name = "UIServiceResponse")
public class UIServiceResponse<T> {

    private UIException exception;
    private List<T> result;

    public UIException getException() {
        return exception;
    }
    public void setException(UIException exception) {
        this.exception = exception;
    }
    public List<T> getResult() {
        return result;
    }
    public void setResult(List<T> result) {
        this.result = result;
    }
}


The problem is, everything works fine if we include
@XmlSeeAlso({Agent.class}) to UIServiceResponse. But when we remove we are
asked to provide information about the return classes.

This UIServiceResponse is used from multiple projects and having @XmlSeeAlso
included for every DTO we create is difficult and not a good approach.

Is there any approach that works without using @XmlSeeAlso and gets derived
class data to client? Any help is really appreciated.

Hope I did not confuse :)
Thank you
-- 
View this message in context: http://cxf.547215.n5.nabble.com/JAXB-only-converting-the-base-class-not-the-derived-class-tp2418721p2418721.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JAXB only converting the base class not the derived class

Posted by Sudheer-3 <su...@gmail.com>.
Pls ignore the below line mentioned part of spring-config.xml. I was trying
some option and got included.

<property name="jaxbElementClassMap" value="" /> 
-- 
View this message in context: http://cxf.547215.n5.nabble.com/JAXB-only-converting-the-base-class-not-the-derived-class-tp2418721p2418856.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JAXB only converting the base class not the derived class

Posted by Sudheer-3 <su...@gmail.com>.
I just extended UIServiceResponse and included @XmlSeeAlso for all the
required beans. This solution worked.
-- 
View this message in context: http://cxf.547215.n5.nabble.com/JAXB-only-converting-the-base-class-not-the-derived-class-tp2418721p2636331.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JAXB only converting the base class not the derived class

Posted by Daniel Kulp <dk...@apache.org>.
Not my area, but I think you can register a custom  
ContextResolver<JAXBContext>  for the service that would allow you to create 
the JAXBContext that is used.   See:
http://cxf.547215.n5.nabble.com/cxf-rest-how-to-deal-with-xs-any-
td2281907.html#a2370139

for an example.

Dan

On Tuesday 10 August 2010 7:21:38 am Sudheer-3 wrote:
> We have implemented RESTFul service using Apache CXF. Used JSON as the
> communication between our GWT client and RESTFul server.
> 
> Our DTOs are having structure Similar to below. Every DTO should extend
> BaseDTO.
> 
> @XmlRootElement(name = "BaseDTO")
> public class BaseDTO implements Serializable {
> 
>     private String UUID;
>     public String getUUID() {
>         return UUID;
>     }
>     public void setUUID(String UUID) {
>         this.UUID = UUID;
>     }
> }
> 
> @XmlRootElement(name = "Agent")
> public class Agent extends BaseDTO {
> 
>     private String description;
>     public String getDescription() {
>         return description;
>     }
>     public void setDescription(String description) {
>         this.description = description;
>     }
> }
> 
> We have configured JSONProvider in spring-config.xml as below
> 
>    <jaxrs:server id="restServer" address="/services/">
>         <jaxrs:serviceBeans>
>             <ref bean="agentService"/>
>         </jaxrs:serviceBeans>
>         <jaxrs:extensionMappings>
>             <entry key="feed" value="application/atom+xml"/>
>             <entry key="json" value="application/json"/>
>             <entry key="xml" value="application/xml"/>
>             <entry key="html" value="text/html"/>
>         </jaxrs:extensionMappings>
> 
>         <jaxrs:providers>
>             <bean class="org.apache.cxf.jaxrs.provider.JSONProvider">
>                 <property name="writeXsiType" value="false" />
>                 <property name="readXsiType" value="false" />
>                 <property name="jaxbElementClassMap" value="" />
>             </bean>
>         </jaxrs:providers>
>     </jaxrs:server>
> 
> 
> Methods from AgentService.java returns List<Agent> and we put this in a
> common response format defined below and constructs response using Response
> (javax.ws.rs.core.Response.ok(UIServiceResponse<T>)).
> 
> @XmlRootElement(name = "UIServiceResponse")
> public class UIServiceResponse<T> {
> 
>     private UIException exception;
>     private List<T> result;
> 
>     public UIException getException() {
>         return exception;
>     }
>     public void setException(UIException exception) {
>         this.exception = exception;
>     }
>     public List<T> getResult() {
>         return result;
>     }
>     public void setResult(List<T> result) {
>         this.result = result;
>     }
> }
> 
> 
> The problem is, everything works fine if we include
> @XmlSeeAlso({Agent.class}) to UIServiceResponse. But when we remove we are
> asked to provide information about the return classes.
> 
> This UIServiceResponse is used from multiple projects and having
> @XmlSeeAlso included for every DTO we create is difficult and not a good
> approach.
> 
> Is there any approach that works without using @XmlSeeAlso and gets derived
> class data to client? Any help is really appreciated.
> 
> Hope I did not confuse :)
> Thank you

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog