zoukankan      html  css  js  c++  java
  • 使用Apache cxf 和Spring在Tomcat下发布Webservice指南

    转载 http://blog.csdn.net/zhangzhaokun/article/details/4750021 

    最近学习了如何使用apache cxf和Spring发布webservice,虽然网上的资料很多,但是没有一个文档可以让读者按照操作步骤来实现完整的发布流程,都需要多篇文件杂合在一起,互相参考才可以完成第一个HelloWorld形式的Webservice。现在将我利用apache cxf和Spring发布webservice的详细的发布过程记录下来,以供后来者参考。

     环境信息如下:

     JDK1.5.15

     Tomcat5.5.26

     Spring2.5.5

     apache-cxf-2.2.4

     具体实现步骤如下:

    (1)使用IDE建立WEB工程cxfservice

    工程目录结构如下:

    其中的WEB-INF/lib目录下的jar包为直接将apache-cxf-2.2.4.zip下载包中的apache-cxf-2.2.4/lib目录下的全部的jar,在学习过程中这种办法是最简单的了。

    当然我们也可以用最少的Jar包来完成本实例的任务,首先要将cxf的依赖包加入,包括如下一些jar包:

    [c-sharp] view plaincopy
    1. commons-logging-1.1.1.jar  
    2. geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)  
    3. geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)  
    4. geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)  
    5. geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)  
    6. geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)  
    7. geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)  
    8. geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)  
    9. jaxb-api-2.1.jar  
    10. jaxb-impl-2.1.12.jar  
    11. jetty-6.1.21.jar  
    12. jetty-util-6.1.21.jar  
    13. neethi-2.0.4.jar  
    14. saaj-api-1.3.jar  
    15. saaj-impl-1.3.2.jar  
    16. wsdl4j-1.6.2.jar  
    17. wstx-asl-3.2.8.jar  
    18. XmlSchema-1.4.5.jar  
    19. xml-resolver-1.2.jar  
      

    再就是Spring的包了,包括如下一些

    [c-sharp] view plaincopy
    1. aopalliance-1.0.jar  
    2. spring-core-2.5.5.jar  
    3. spring-beans-2.5.5.jar  
    4. spring-context-2.5.5.jar  
    5. spring-web-2.5.5.jar  

    最后就是apache cxf本身的包了

    [c-sharp] view plaincopy
    1. cxf-2.2.3.jar  

     

    (2)配置文件说明

    applicationContext.xml文件的内容如下:

    [c-sharp] view plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans  
    3.     xmlns="http://www.springframework.org/schema/beans"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
    6.     <import resource="classpath*:META-INF/cxf/cxf.xml" />  
    7.     <import resource="classpath*:META-INF/cxf/cxf-extension-soap.xml" />  
    8.     <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />  
    9.     <import resource="classpath:services.xml" />   
    10. </beans>  

    services.xml文件的内容如下:

    [c-sharp] view plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
    5.     xsi:schemaLocation="  
    6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
    7. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
    8.     <jaxws:endpoint id="webServiceHelloWorld"  
    9.         address="/HelloWorld" implementor="com.cxf.test.interfaces.HelloWorldImpl"/>  
    10. </beans>  

    web.xml文件的内容如下:

    [c-sharp] view plaincopy
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
    3.     <display-name>cxfservice</display-name>  
    4.     <context-param>  
    5.         <param-name>contextConfigLocation</param-name>  
    6.         <param-value>WEB-INF/classes/applicationContext.xml</param-value>  
    7.     </context-param>  
    8.     <listener>  
    9.         <listener-class>  
    10.             org.springframework.web.context.ContextLoaderListener  
    11.         </listener-class>  
    12.     </listener>  
    13.     <servlet>  
    14.         <servlet-name>CXFServlet</servlet-name>  
    15.         <servlet-class>  
    16.             org.apache.cxf.transport.servlet.CXFServlet  
    17.         </servlet-class>  
    18.     </servlet>  
    19.     <servlet-mapping>  
    20.         <servlet-name>CXFServlet</servlet-name>  
    21.         <url-pattern>/services/*</url-pattern>  
    22.     </servlet-mapping>  
    23.       
    24.     <welcome-file-list>  
    25.         <welcome-file>index.html</welcome-file>  
    26.         <welcome-file>index.htm</welcome-file>  
    27.         <welcome-file>index.jsp</welcome-file>  
    28.         <welcome-file>default.html</welcome-file>  
    29.         <welcome-file>default.htm</welcome-file>  
    30.         <welcome-file>default.jsp</welcome-file>  
    31.     </welcome-file-list>  
    32. </web-app>  

    (3)发布的HelloWord服务说明

        要发布的HelloWorld服务的接口定义文件com.cxf.test.interfaces.HelloWorld:

    1. package com.cxf.test.interfaces;  
    2.   
    3. import javax.jws.WebParam;  
    4. import javax.jws.WebResult;  
    5. import javax.jws.WebService;  
    6.   
    7. @WebService  
    8. public interface HelloWorld  
    9. {  
    10.     /* 
    11.      * 一个简单的方法,返回一个字符串 
    12.      *  
    13.      * @param hello 
    14.      *  
    15.      * @return 
    16.      */  
    17.     String say(String hello);  
    18.   
    19.     /** 
    20.      * 稍微复杂一些的方法,传递一个对象给服务端处理 
    21.      *  
    22.      * @param user 
    23.      * @return 
    24.      */  
    25.     String sayUserName(@WebParam(name = "user") UserDTO user);  
    26.   
    27.     /** 
    28.      * 最复杂的方法,返回一个List封装的对象集合 
    29.      *  
    30.      * @return 
    31.      */  
    32.     public @WebResult(partName = "o") ListObject findUsers();  
    33. }  

          要发布的HelloWorld服务的接口实现类com.cxf.test.interfaces.HelloWorldImpl:

    1. package com.cxf.test.interfaces;  
    2.   
    3. import java.util.ArrayList;  
    4.   
    5. import javax.jws.WebService;  
    6.   
    7. /** 
    8.  * @author zhangzk 
    9.  * 
    10.  */  
    11. /** 
    12.  * WebService实现类. 
    13.  *  
    14.  * 使用@WebService指向Interface定义类即可. 
    15.  */  
    16. @WebService(endpointInterface = "com.cxf.test.interfaces.HelloWorld")  
    17. public class HelloWorldImpl implements HelloWorld  
    18. {  
    19.     public String sayUserName(UserDTO user)  
    20.     {  
    21.         return "hello " + user.getName();  
    22.     }  
    23.   
    24.     public String say(String hello)  
    25.     {  
    26.         return "hello " + hello;  
    27.     }  
    28.   
    29.     public ListObject findUsers()  
    30.     {  
    31.         ArrayList<Object> list = new ArrayList<Object>();  
    32.   
    33.         list.add(instancUser(1, "lib"));  
    34.         list.add(instancUser(2, "mld"));  
    35.         list.add(instancUser(3, "lq"));  
    36.         list.add(instancUser(4, "gj"));  
    37.         ListObject o = new ListObject();  
    38.         o.setList(list);  
    39.         return o;  
    40.     }  
    41.   
    42.     private UserDTO instancUser(Integer id, String name)  
    43.     {  
    44.         UserDTO user = new UserDTO();  
    45.         user.setId(id);  
    46.         user.setName(name);  
    47.         return user;  
    48.     }  
    49. }  

           findUsers()接口返回的参数对象定义文件com.cxf.test.interfaces.ListObject:

    [c-sharp] view plaincopy
    1. package com.cxf.test.interfaces;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5.   
    6. import javax.xml.bind.annotation.XmlAccessType;  
    7. import javax.xml.bind.annotation.XmlAccessorType;  
    8. import javax.xml.bind.annotation.XmlElement;  
    9. import javax.xml.bind.annotation.XmlType;  
    10.   
    11. @XmlAccessorType(XmlAccessType.FIELD)  
    12. @XmlType(name = "listObject", propOrder ={ "list" })  
    13. public class ListObject  
    14. {  
    15.     @XmlElement(nillable = true)  
    16.     protected List<Object> list;  
    17.   
    18.     /** 
    19.      * Gets the value of the list property. 
    20.      *  
    21.      * <p> 
    22.      * This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you make to the returned list will be 
    23.      * present inside the JAXB object. This is why there is not a <CODE>set</CODE> method for the list property. 
    24.      *  
    25.      * <p> 
    26.      * For example, to add a new item, do as follows: 
    27.      *  
    28.      * <pre> 
    29.      * getList().add(newItem); 
    30.      * </pre> 
    31.      *  
    32.      *  
    33.      * <p> 
    34.      * Objects of the following type(s) are allowed in the list {@link Object } 
    35.      *  
    36.      *  
    37.      */  
    38.     public List<Object> getList()  
    39.     {  
    40.         if (list == null)  
    41.         {  
    42.             list = new ArrayList<Object>();  
    43.         }  
    44.         return this.list;  
    45.     }  
    46.   
    47.     public void setList(ArrayList<Object> list)  
    48.     {  
    49.         this.list = list;  
    50.     }  
    51.   
    52. }  

            UserDTO instancUser(Integer id, String name)接口返回的对象定义文件com.cxf.test.interfaces.UserDTO:

    [c-sharp] view plaincopy
    1. package com.cxf.test.interfaces;  
    2.   
    3. import javax.xml.bind.annotation.XmlAccessType;  
    4. import javax.xml.bind.annotation.XmlAccessorType;  
    5. import javax.xml.bind.annotation.XmlType;  
    6.   
    7. /** 
    8.  * Web Service传输User信息的DTO. 
    9.  *  
    10.  * 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响. 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定. 
    11.  *  
    12.  */  
    13. @XmlAccessorType(XmlAccessType.FIELD)  
    14. @XmlType(name = "User")  
    15. public class UserDTO  
    16. {  
    17.   
    18.     protected Integer id;  
    19.   
    20.     protected String name;  
    21.   
    22.     public Integer getId()  
    23.     {  
    24.         return id;  
    25.     }  
    26.   
    27.     public void setId(Integer value)  
    28.     {  
    29.         id = value;  
    30.     }  
    31.   
    32.     public String getName()  
    33.     {  
    34.         return name;  
    35.     }  
    36.   
    37.     public void setName(String value)  
    38.     {  
    39.         name = value;  
    40.     }  
    41. }  

    (4)将WEB工程发布到Tomcat下作为一个WEB应用,webContext为cxfservice,Port为9000

           启动Tomcat后,以如下方式访问http://localhost:9000/cxfservice/services/HelloWorld?wsdl即可看到我们发布的Webservices服务HelloWorld了。在浏览器中将看到的WSDL文件另存为HelloWorld.xml即为发布的Webservice的WSDL文件。后续的调用过程与其它的操作方式完全相同。  

  • 相关阅读:
    Java并发编程:synchronized
    对一致性Hash算法,Java代码实现的深入研究
    在Xcode中使用Git进行源码版本控制
    这些 Git 技能够你用一年了
    简明 Git 命令速查表
    Git 分支管理是一门艺术
    2016年Web前端面试题目汇总
    iOS 面试基础题目
    Json数据交互格式介绍和比较
    SpringBoot端口和访问路径
  • 原文地址:https://www.cnblogs.com/chenying99/p/2781070.html
Copyright © 2011-2022 走看看