开发者

How do I get a CXF client to understand a List?

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?


One option is to inject a WebClient and to use, in this case,

webClient.getCollection(MyBean.class);

As far as this exception is concerned:

What CXF version are you using ? We have a lot of tests for reading explicit collections...

Is MBean not qualified as XMLRootElement ?


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.


List<Object> providers = new ArrayList<Object>();
providers.add( new JacksonJaxbJsonProvider() );

WebClient  client=WebClient.create("http://localhost:6969/CXF3/rest",providers);

client = client.accept("application/json").type("application/json").path("/service/getAll");

Collection<? extends Person> order=client.getCollection(Person.class);

for(Person p:order){
  System.out.println(p.getEname()+" "+p.getEmpid()+" "+p.getEsal());
}


This issue is fixed in CXF, qualify MBean class as a workround

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜