I’m using Apache CXF for my restful web services. I have a service defined by an interface that returns a list of my bean.
@Path("/")
@Produces("application/xml")
public interface MyService {
@GET
@Path("/test")
public List<MyBean> getBeans() throws IOException;
}
..and the service implementation is as such;
public class MyServiceImpl implements MyService {
public List<MyBean> getBeans() {
ArrayList<MyBean> beans = new ArrayList<MyBean>();
beans.add(new MyBean("foo", "bar");
return beans;
}
}
This is deployed on my server and works fine. I can hit the service in my browser and get the result I’d expect. The problem is when I try to get a CXF client to call the service.
In my client app I declare a client with the following spring config;
<jaxrs:client id="myClient" inheritHeaders="true"
address="myhost/test"
serviceClass="com.example.MyService">
<jaxrs:headers>
<entry key="Accept" value="application/xml"/>
</jaxrs:headers>
<jaxrs:providers>
<ref bean="myJaxbXmlProvider"/>
<ref bean="myJsonProvider"/>
</jaxrs:providers>
</jaxrs:client>
<bean id="myJaxbXmlProvider" class="org.apache.cxf.jaxrs.provider.JAXBElementProvider">
<property name="jaxbElementClassMap" ref="myElementClassMap"/>
</bean>
<bean id="myJsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider">
<property name="jaxbElementClassMap" ref="myElementClassMap"/>
</bean>
<util:map id="myElementClassMap">
<entry key="com.example.MyBean" value="bean"/>
</util:map>
When the client is invoked I get this stacktrace;
org.apache.cxf.jaxrs.client.ClientWebApplicationException:
.Problem with reading the response
message, class : interface
java.util.List, ContentType :
application/xml.
….
Caused by:
javax.ws.rs.WebApplicationException:
java.lang.ClassCastException:
com.example.MyBean
cannot be cast to
org.apache.cxf.jaxrs.provider.AbstractJAXBProvider$CollectionWrapper
Any ideas?
There could be a bug in CXF JAX-RS to do with reading explicit collections of beans which have no @XmlRootElement annotations, looking into it.