如何让 CXF 客户端理解列表?

发布于 2024-11-07 05:51:36 字数 1983 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青朷 2024-11-14 05:51:36

一种选择是注入 WebClient 并使用,在本例中,

webClient.getCollection(MyBean.class);

就此异常而言:

您正在使用什么 CXF 版本?
我们有很多读取显式集合的测试...

MBean 不符合 XMLRootElement 资格吗?

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 ?

冷…雨湿花 2024-11-14 05:51:36

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.

柠北森屋 2024-11-14 05:51:36
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());
}
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());
}
不喜欢何必死缠烂打 2024-11-14 05:51:36

此问题已在 CXF 中修复,将 MBean 类限定为解决方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文