如何让 CXF 客户端理解列表?
我正在使用 Apache CXF 来实现宁静的 Web 服务。我有一个由接口定义的服务,该接口返回我的 bean 列表。
@Path("/")
@Produces("application/xml")
public interface MyService {
@GET
@Path("/test")
public List<MyBean> getBeans() throws IOException;
}
..服务实现就是这样;
public class MyServiceImpl implements MyService {
public List<MyBean> getBeans() {
ArrayList<MyBean> beans = new ArrayList<MyBean>();
beans.add(new MyBean("foo", "bar");
return beans;
}
}
这是部署在我的服务器上并且工作正常。我可以在浏览器中访问该服务并获得我期望的结果。问题是当我尝试让 CXF 客户端调用该服务时。
在我的客户端应用程序中,我使用以下 spring 配置声明一个客户端;
<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>
当调用客户端时,我得到这个堆栈跟踪;
org.apache.cxf.jaxrs.client.ClientWebApplicationException: .读取响应时出现问题 消息,类:接口 java.util.List,内容类型: 应用程序/xml。
....
造成原因: javax.ws.rs.WebApplicationException: java.lang.ClassCastException: com.example.MyBean 无法投射到 org.apache.cxf.jaxrs.provider.AbstractJAXBProvider$CollectionWrapper
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种选择是注入 WebClient 并使用,在本例中,
就此异常而言:
您正在使用什么 CXF 版本?
我们有很多读取显式集合的测试...
MBean 不符合 XMLRootElement 资格吗?
One option is to inject a WebClient and to use, in this case,
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 ?
CXF JAX-RS 中可能存在一个错误,与读取没有 @XmlRootElement 注释的显式 Bean 集合有关,请对其进行调查。
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.
此问题已在 CXF 中修复,将 MBean 类限定为解决方法
This issue is fixed in CXF, qualify MBean class as a workround