Apache CXF Web服务问题 [英] Apache CXF web services problems

查看:115
本文介绍了Apache CXF Web服务问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Maven的多模块项目。在其中一个模块中,我有几个使用Apache CXF Framework 2.5.4开发的Web服务。目前我有两个问题或问题。

I have a multi-module project using Maven. On one of the modules I have several web services developed using Apache CXF Framework 2.5.4. At the moment I have two "problems" or questions.

首先,如果我调用其中一个应返回List的Web服务的方法,列表为空,它返回null而不是空列表。
我试图找出可能存在的问题,如果它是我正在使用的CXF版本的错误,或者我应该使用一些注释来修改方法或响应的定义,但我无法'找不到任何东西。我见过一些人有同样的问题,但没有解决方案。

First of all, if I call to a method of one of the web services that should return a List, if the list is empty, it returns "null" instead of the empty list. I was trying to find out what could be the problem, if it's a bug of the CXF version I'm using or if I should use some annotation to modify the definition of the method or the response, but I couldn't find anything. I've seen some people with the same problem, but no solution.

我想问的另一件事是:我正在使用MVC模式开发一个Web应用程序。我想知道我应该从Controller调用Web服务的方式,而不是使用ClasspathXmlCpplicationContext然后使用context.getBean()。

The other thing I wanted to ask is: I'm developing a web application using MVC pattern. I'm wondering which way I should call the web service from the Controller instead of using ClasspathXmlCpplicationContext and then context.getBean().

例如,一个bean的bean定义客户端的Web服务是:

For example, the bean definition for one of the web services on the client side is:

<jaxws:client id="deviceWSClient"
        serviceClass="..IDeviceWebService"
        address="http://localhost:8080/../DeviceWS" /> 

我已经尝试过@Autowired或@WebServiceRef注释。有了这些它可以工作,但没有对Web服务做HTTP请求,我想它从本地存储库获取依赖。我认为我需要的是在Controller上注入这个bean的方法。

I've already tried usin @Autowired or @WebServiceRef annotations. With these it works but not doing a HTTP request to the web service, I guess it gets the dependency from the local repository. I think what I need is the way of injecting this bean on the Controller.

推荐答案

回答你的问题

对于您的第一个问题:如果列表为空,则由CXF版本2.6.1正确处理 - 服务返回空。只是为了证明我有一个样本服务,其中类型以这种方式定义:

For your first question: If the list is empty it is correctly handled by CXF version 2.6.1 - the service returns a empty. Just to demonstrate I have a sample service where types are defined this way:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "MemberSearchResponse", namespace="http://bk.org/memberservice/" )
public class MemberSearchResponse {

    @XmlElementWrapper(name="memberDetails")
    private List<MemberDetail> memberDetails;

如果我在上面返回一个空的memberDetails,那么通过网络的xml是这样的:

If I return a empty memberDetails above, the xml that goes over the wire is this:

    <ns2:searchMemberResponse xmlns:ns2="http://bk.org/memberservice/">
        <ns2:MemberSearchResponse>
           <memberDetails/>
        </ns2:MemberSearchResponse>
    </ns2:searchMemberResponse>

编辑

它正确地作为上面的包装器类型的一部分处理,但是如果不是返回包装器类型,则返回null,直接返回列表。

It is correctly handled as part of a wrapper type like above, but DOES return null if instead of returning a wrapper type, the list is directly returned.

考虑一个Web服务接口以这种方式定义:

Consider a Webservice interface defined this way:

@WebMethod(operationName = "searchMember")
    List<MemberDetail> searchMember(@WebParam(name = "MemberSearchRequest") MemberSearchRequest memberSearchRequest);

如果返回的List是一个空列表,它也会被CXF 2.6.1序列化为null。

If the List returned is an Empty list, it gets serialized as null by CXF 2.6.1 also.

解决方法是使用包装类型

编辑END

第二个问题:

您正在创建一个客户端bean这样:

You are creating a client bean this way:

<jaxws:client id="deviceWSClient"
        serviceClass="..IDeviceWebService"
        address="http://localhost:8080/../DeviceWS" /> 

一旦你用这种方式创建了一个Spring bean,就可以像普通的Spring bean一样对待它以你对任何普通Spring bean的方式注入它,例如,以这种方式注入:

Once you have created a Spring bean this way, you can treat it just like a normal Spring bean and inject it the way you would do with any normal Spring bean, for eg, either inject it this way:

 <bean id="consumerBean" class="...">
    <property name="deviceWS" ref="deviceWSClient">
 </bean>

或使用 @Autowired

@Autowired IDWebService deviceWSClient

或者用户 @Resource

@Resource IDWebService deviceWSClient

这些是注入bean的常用方法。

These are the usual ways of injecting in a bean.

我在这个github位置有一个示例应用程序,您可以使用:
https ://github.com/bijukunjummen/memberservice-codefirst.git

I have a sample application at this github location that you can play with: https://github.com/bijukunjummen/memberservice-codefirst.git

使用启动服务器mvn tomcat:run 并运行测试 org.bk.memberservice.TestCxfIntegrationTest ,它将向CXF服务发出请求。

Just start up the server using mvn tomcat:run and run a test org.bk.memberservice.TestCxfIntegrationTest which will make a request to the CXF service.

这篇关于Apache CXF Web服务问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆