`
zhang_zling
  • 浏览: 96436 次
  • 性别: Icon_minigender_2
  • 来自: 济南
社区版块
存档分类
最新评论

利用cxf开发WebService

阅读更多
今天早上Google了一下Spring找到不Bean的一个异常,却让发现了一大财宝,觉得实用就贴到这了.
转载地址:http://www.iteye.com/topic/170620

    现在的项目中需要用到SOA概念的地方越来越多,最近接手的一个项目中就提出了这样的业务要求,需要在.net开发的客户端系统中访问java开发的web系统,这样的业务需求自然需要通过WebService进行信息数据的操作。下面就将我们在开发中摸索的一点经验教训总结以下,以供大家参考.

在WebService开发笔记 2 -- VS 2005 访问WebServcie更简单中作一个跨平台访问WebServcie服务的例子....

在WebService开发笔记 3 -- 增强访问 WebService 的安全性通过一个简单的用户口令验证机制来加强一下WebService的安全性....

我们项目的整个架构使用的比较流行的WSH MVC组合,即webwork2 + Spring + Hibernate;
1.首先集成Apacha CXF WebService 到 Spring 框架中;
   apache cxf 下载地址:http://people.apache.org/dist/incubator/cxf/2.0.4-incubator/apache-cxf-2.0.4-incubator.zip
  在spring context配置文件中引入以下cxf配置
Xml代码
<import resource="classpath*:META-INF/cxf/cxf.xml" /> 
<import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" /> 
<import resource="classpath*:META-INF/cxf/cxf-servlet.xml" /> 

<import resource="classpath*:META-INF/cxf/cxf.xml" />
<import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />

   在web.xml中添加过滤器:
Xml代码
<servlet> 
    <servlet-name>CXFServlet</servlet-name> 
    <servlet-class> 
        org.apache.cxf.transport.servlet.CXFServlet  
    </servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>CXFServlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
</servlet-mapping> 

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>


2.开发服务端WebService接口:
Java代码
/** 
* WebService接口定义类. 
*  
* 使用@WebService将接口中的所有方法输出为Web Service. 
* 可用annotation对设置方法、参数和返回值在WSDL中的定义. 
*/ 
@WebService 
public interface WebServiceSample {  
 
 
    /** 
     * 一个简单的方法,返回一个字符串 
     * @param hello 
     * @return 
     */ 
    String say(String hello);  
      
    /** 
     * 稍微复杂一些的方法,传递一个对象给服务端处理 
     * @param user 
     * @return 
     */ 
    String sayUserName(  
            @WebParam(name = "user")   
            UserDTO user);  
      
    /** 
     * 最复杂的方法,返回一个List封装的对象集合 
     * @return 
     */ 
    public   
    @WebResult(partName="o")  
    ListObject findUsers();  
 


/**
* WebService接口定义类.
*
* 使用@WebService将接口中的所有方法输出为Web Service.
* 可用annotation对设置方法、参数和返回值在WSDL中的定义.
*/
@WebService
public interface WebServiceSample {


/**
* 一个简单的方法,返回一个字符串
* @param hello
* @return
*/
String say(String hello);

/**
* 稍微复杂一些的方法,传递一个对象给服务端处理
* @param user
* @return
*/
String sayUserName(
@WebParam(name = "user")
UserDTO user);

/**
* 最复杂的方法,返回一个List封装的对象集合
* @return
*/
public
@WebResult(partName="o")
ListObject findUsers();

}

由简单到复杂定义了三个接口,模拟业务需求;

3.实现接口
Java代码
/** 
* WebService实现类. 
*  
* 使用@WebService指向Interface定义类即可. 
*/ 
@WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")  
public class WebServiceSampleImpl implements WebServiceSample {  
 
    public String sayUserName(UserDTO user) {  
        return "hello "+user.getName();  
    }  
 
    public String say(String hello) {  
        return "hello "+hello;  
    }  
 
    public ListObject findUsers() {  
        ArrayList<Object> list = new ArrayList<Object>();  
          
        list.add(instancUser(1,"lib"));  
        list.add(instancUser(2,"mld"));  
        list.add(instancUser(3,"lq"));  
        list.add(instancUser(4,"gj"));  
        ListObject o = new ListObject();  
        o.setList(list);  
        return o;  
    }  
      
    private UserDTO instancUser(Integer id,String name){  
        UserDTO user = new UserDTO();  
        user.setId(id);  
        user.setName(name);  
        return user;  
    }  


/**
* WebService实现类.
*
* 使用@WebService指向Interface定义类即可.
*/
@WebService(endpointInterface = "cn.org.coral.biz.examples.webservice.WebServiceSample")
public class WebServiceSampleImpl implements WebServiceSample {

public String sayUserName(UserDTO user) {
return "hello "+user.getName();
}

public String say(String hello) {
return "hello "+hello;
}

public ListObject findUsers() {
ArrayList<Object> list = new ArrayList<Object>();

list.add(instancUser(1,"lib"));
list.add(instancUser(2,"mld"));
list.add(instancUser(3,"lq"));
list.add(instancUser(4,"gj"));
ListObject o = new ListObject();
o.setList(list);
return o;
}

private UserDTO instancUser(Integer id,String name){
UserDTO user = new UserDTO();
user.setId(id);
user.setName(name);
return user;
}
}


4.依赖的两个类:用户对象与List对象
Java代码
/** 
* Web Service传输User信息的DTO. 
*  
* 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响. 
* 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定. 
*  
*/ 
@XmlAccessorType(XmlAccessType.FIELD)  
@XmlType(name = "User")  
public class UserDTO {  
 
    protected Integer id;  
 
    protected String name;  
 
    public Integer getId() {  
        return id;  
    }  
 
    public void setId(Integer value) {  
        id = value;  
    }  
 
    public String getName() {  
        return name;  
    }  
 
    public void setName(String value) {  
        name = value;  
    }  


/**
* Web Service传输User信息的DTO.
*
* 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响.
* 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "User")
public class UserDTO {

protected Integer id;

protected String name;

public Integer getId() {
return id;
}

public void setId(Integer value) {
id = value;
}

public String getName() {
return name;
}

public void setName(String value) {
name = value;
}
}

关于List对象,参照了有关JWS的一个问题中的描述:DK6.0 自带的WebService中 WebMethod的参数好像不能是ArrayList 或者其他List
传递List需要将List 包装在其他对象内部才行 (个人理解 如有不对请指出) ,我在实践中也遇到了此类问题.通过以下封装的对象即可以传递List对象.
Java代码
/** 
* <p>Java class for listObject complex type. 
*  
* <p>The following schema fragment specifies the expected content contained within this class. 
*  
* <pre> 
* <complexType name="listObject"> 
*   <complexContent> 
*     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*       <sequence> 
*         <element name="list" type="{http://www.w3.org/2001/XMLSchema}anyType" maxOccurs="unbounded" minOccurs="0"/> 
*       </sequence> 
*     </restriction> 
*   </complexContent> 
* </complexType> 
* </pre> 
*  
*  
*/ 
@XmlAccessorType(XmlAccessType.FIELD)  
@XmlType(name = "listObject", propOrder = { "list" })  
public class ListObject {  
 
    @XmlElement(nillable = true)  
    protected List<Object> list;  
 
    /** 
     * Gets the value of the list property. 
     *  
     * <p> 
     * This accessor method returns a reference to the live list, 
     * not a snapshot. Therefore any modification you make to the 
     * returned list will be present inside the JAXB object. 
     * This is why there is not a <CODE>set</CODE> method for the list property. 
     *  
     * <p> 
     * For example, to add a new item, do as follows: 
     * <pre> 
     *    getList().add(newItem); 
     * </pre> 
     *  
     *  
     * <p> 
     * Objects of the following type(s) are allowed in the list 
     * {@link Object } 
     *  
     *  
     */ 
    public List<Object> getList() {  
        if (list == null) {  
            list = new ArrayList<Object>();  
        }  
        return this.list;  
    }  
 
    public void setList(ArrayList<Object> list) {  
        this.list = list;  
    }  
 


/**
* <p>Java class for listObject complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="listObject">
*   <complexContent>
*     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
*       <sequence>
*         <element name="list" type="{http://www.w3.org/2001/XMLSchema}anyType" maxOccurs="unbounded" minOccurs="0"/>
*       </sequence>
*     </restriction>
*   </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "listObject", propOrder = { "list" })
public class ListObject {

@XmlElement(nillable = true)
protected List<Object> list;

/**
* Gets the value of the list property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the list property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
*    getList().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Object }
*
*
*/
public List<Object> getList() {
if (list == null) {
list = new ArrayList<Object>();
}
return this.list;
}

public void setList(ArrayList<Object> list) {
this.list = list;
}

}


5.WebService 服务端 spring 配置文件 ws-context.xml
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd" 
    default-autowire="byName" default-lazy-init="true"> 
      
    <jaxws:endpoint id="webServiceSample" 
        address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/> 
 
</beans> 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">

<jaxws:endpoint id="webServiceSample"
address="/WebServiceSample" implementor="cn.org.coral.biz.examples.webservice.WebServiceSampleImpl"/>

</beans>


WebService 客户端 spring 配置文件 wsclient-context.xml
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" 
    xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd" 
    default-autowire="byName" default-lazy-init="true"> 
 
    <!-- ws client --> 
    <bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService" 
        factory-bean="identityValidateServiceClientFactory" factory-method="create" /> 
 
    <bean id="identityValidateServiceClientFactory" 
        class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> 
        <property name="serviceClass" 
            value="cn.org.coral.admin.service.IdentityValidateService" /> 
        <property name="address" 
            value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/> 
    </bean> 
      
</beans> 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName" default-lazy-init="true">

<!-- ws client -->
<bean id="identityValidateServiceClient" class="cn.org.coral.admin.service.IdentityValidateService"
factory-bean="identityValidateServiceClientFactory" factory-method="create" />

<bean id="identityValidateServiceClientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="cn.org.coral.admin.service.IdentityValidateService" />
<property name="address"
value="http://88.148.29.54:8080/coral/services/IdentityValidateService"/>
</bean>

</beans>

6.发布到tomcat服务器以后通过以下地址即可查看自定义的webservice接口生成的wsdl:
http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl

7.调用WebService接口的Junit单元测试程序
Java代码
package test.coral.sample;  
 
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;  
 
import cn.org.coral.biz.examples.webservice.WebServiceSample;  
import cn.org.coral.biz.examples.webservice.dto.UserDTO;  
 
public class TestWebServiceSample extends 
        AbstractDependencyInjectionSpringContextTests {  
    WebServiceSample webServiceSampleClient;  
 
    public void setWebServiceSampleClient(WebServiceSample webServiceSampleClient) {  
        this.webServiceSampleClient = webServiceSampleClient;  
    }  
      
    @Override 
    protected String[] getConfigLocations() {  
        setAutowireMode(AUTOWIRE_BY_NAME);  
                  //spring 客户端配置文件保存位置  
        return new String[] { "classpath:/cn/org/coral/biz/examples/webservice/wsclient-context.xml" };  
    }  
      
    public void testWSClinet(){  
        Assert.hasText(webServiceSampleClient.say(" world"));  
    }  
分享到:
评论

相关推荐

    cxf开发webservice客户端和服务器端文档

    cxf开发webservice客户端和服务器端 cxf(Apache CXF)是一款开源的WebService框架,用于开发webservice客户端和服务器端。它提供了许多有用的功能,如自动生成服务桩、客户调用桩和WSDL文档等,使得开发webservice...

    cxf开发webservice所用jar包

    这个标题“cxf开发webservice所用jar包”表明我们正在讨论一组CXF框架所需的JAR文件,这些文件对于利用CXF开发基于Web服务的应用程序至关重要。在描述中提到“cxf-2.4.1整合spring3.0所用jar包”,这暗示了我们使用...

    cxf 开发webservice客户端

    总结,利用CXF开发Web服务客户端涉及了从生成客户端代码、创建实例到测试的整个流程。了解这些知识点,能够帮助我们更高效地构建和测试与Web服务交互的应用程序。在实际项目中,还需要根据具体需求进行相应的调整和...

    利用cxf实现webservice

    【标题】:“利用CXF实现WebService” 在Java世界中,CXF是一个强大的开源框架,用于构建和开发Web服务。它支持多种Web服务标准,包括SOAP、RESTful API以及WS-*协议栈。CXF使得开发者能够方便地创建和消费Web服务...

    使用cxf 开发webService 初体验

    本篇文章将探讨如何利用CXF来创建和消费Web Service,帮助开发者更好地理解和运用这个工具。 **一、CXF简介** Apache CXF是一个开放源码的Web服务框架,它允许开发者构建和部署SOAP和RESTful Web Services。CXF集成...

    使用CXF开发WebService

    在开发Web服务时,Apache CXF是一个非常强大的框架,它提供了与Spring的紧密集成,使得开发者可以方便地通过注解和Spring配置来暴露和消费Web服务。本篇内容将深入探讨如何使用CXF和Spring来创建和使用Web服务。 ...

    cxf开发webservice服务端

    工具标签则意味着CXF与其他工具(如IDE、构建工具、调试器等)的集成,以及如何利用CXF提供的各种工具(如WSDL生成器、测试客户端等)进行开发和测试。 【压缩包子文件的文件名称列表】:cxf-test 这个文件名暗示...

    基于cxf 的webService 接口开发及调用步骤文档

    通过本文档的介绍,我们了解到如何利用Apache CXF框架快速搭建一个基于Java的WebService服务。这种服务不仅可以用于企业内部系统的通信,还可以作为对外提供服务的一种方式。在实际应用过程中,还需根据具体需求进行...

    使用CXF和camel-cxf调用webservice

    通过使用camel-cxf,你可以利用Camel的灵活性和路由能力来处理Web服务的调用和响应。 在camel-cxf中,你可以: 1. **定义路由**:使用Camel的DSL(Domain Specific Language)或者XML配置文件,定义从何处获取输入...

    使用CXF开发WebService服务器端和客户端

    在IT行业中,Web服务是一种广泛使用的通信协议,它允许不同系统之间通过互联网交换数据。Apache CXF是一个开源框架,专门用于构建和消费Web服务。...通过学习和实践,我们可以利用CXF构建高效、可扩展的分布式系统。

    02.CXF功能概述_CXF发展历史和使用CXF开发WebService服务器端

    【标题】"02.CXF功能概述_CXF发展历史和使用CXF开发WebService服务器端"主要探讨了Apache CXF框架在Web服务领域的应用及其发展历程,同时也涵盖了如何利用CXF来构建一个高效的WebService服务器端。 Apache CXF是一...

    cxf开发webservice与spring整合所需jar包

    下面我们将详细探讨在CXF开发Web服务并与Spring整合时,这些特定jar包的作用。 1. **cxf-2.7.1.jar**:这是CXF的核心库,包含了处理Web服务请求和响应的基本组件,如SOAP消息处理、WSDL生成和绑定、WS-Security等。...

    运用spring和CXF开发webservice

    通过"使用CXF开发WebService.doc"文档,你将能更深入地了解如何具体操作这些步骤,包括详细的XML配置和代码示例。而"cxf-ws"可能是一个包含CXF相关配置和样例代码的文件夹,帮助你更好地理解和实践。 总结来说,...

    cxf开发webservice客户端

    总结来说,使用Apache CXF开发Webservice客户端涉及从获取WSDL、生成客户端代码、配置服务代理到实际调用服务的多个步骤。理解这些步骤以及CXF的特性,将有助于你高效地创建和维护Web服务客户端。

    cxf 开发webservice与调用demo

    - 动态部署:利用CXF的动态客户端和服务器API,无需XML配置,直接通过代码创建并发布服务。 四、调用Web服务 调用Web服务可以使用CXF的客户端API或者通过HTTP客户端库,如Apache HttpClient。如果是JAX-WS服务,...

    Spring+CXF 发布WebService服务

    同时,可以利用CXF的客户端模拟工具进行服务的测试。 通过以上步骤,我们就可以成功地使用Spring+CXF发布一个完整的WebService服务。这个过程中,Spring负责管理和依赖注入,而CXF则负责处理Web服务的通信细节,...

    spring整合CXF开发webService接口所需的全部jar包

    在开发基于Java的Web服务时,Spring和Apache CXF是一个常见的组合。Spring作为一个灵活的框架,可以轻松地与CXF集成,提供强大的服务治理和依赖注入功能。Apache CXF则是一个开源的服务框架,用于创建和消费Web服务...

    CXF实现简单的WebService接口开发

    通过阅读相关的博客和实践上述步骤,你可以深入理解CXF的工作原理,并掌握使用CXF开发WebService的基本流程。"利用CXF实现简单的WebService"这个文件名可能包含了一个实际的示例项目,下载后可以作为学习和调试的...

    基于CXF实现WebService开发.pdf

    基于CXF开发WebService时,开发者可以采用不同的前端API,如JAX-WS或CXF提供的简单前端。Apache CXF支持多种传输协议和服务绑定,包括SOAP/HTTP、RESTful HTTP、JMS和文件传输等。 在开发过程中,可以通过Maven项目...

Global site tag (gtag.js) - Google Analytics