1.创建项目

[plain] view plaincopy
 
  1. mvn archetype:generate -DarchetypeCatalog=Internal  

选择19,创建web项目

2.生成eclipse项目,参见文章
3.修改web.xml

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  6.     id="WebApp_ID" version="2.5">  
  7.     <display-name>Archetype Created Web Application</display-name>  
  8.     <listener>  
  9.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  10.     </listener>  
  11.     <!-- 设置Spring容器加载配置文件路径 -->  
  12.     <context-param>  
  13.         <param-name>contextConfigLocation</param-name>  
  14.         <param-value>WEB-INF/applicationContext.xml</param-value>  
  15.     </context-param>  
  16.   
  17.     <listener>  
  18.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  19.     </listener>  
  20.   
  21.     <servlet>  
  22.         <servlet-name>CXFService</servlet-name>  
  23.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  24.     </servlet>  
  25.   
  26.     <servlet-mapping>  
  27.         <servlet-name>CXFService</servlet-name>  
  28.         <url-pattern>/ws/*</url-pattern>  
  29.     </servlet-mapping>  
  30. </web-app>  

4.创建webservice接口

[java] view plaincopy
 
  1. package com.sysware.demo.app.service.inf;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.   
  8. import com.sysware.demo.app.model.RetInfo;  
  9.   
  10. @WebService  
  11. public interface IGetInfoService  
  12. {  
  13.     @WebMethod(operationName = "add")  
  14.     @WebResult(name = "result")  
  15.     public int add(@WebParam(name = "num1"int num1,  
  16.             @WebParam(name = "num2"int num2);  
  17.       
  18.     @WebMethod(operationName = "getRetInfo")  
  19.     @WebResult(name = "result")  
  20.     public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age"int age);  
  21. }  

5.创建webservice实现类

[java] view plaincopy
 
  1. package com.sysware.demo.app.service.impl;  
  2.   
  3. import javax.jws.WebService;  
  4.   
  5. import com.sysware.demo.app.model.RetInfo;  
  6. import com.sysware.demo.app.service.inf.IGetInfoService;  
  7.   
  8. @WebService(endpointInterface = "com.sysware.demo.app.service.inf.IGetInfoService")  
  9. public class GetInfoServiceImpl implements IGetInfoService  
  10. {  
  11.   
  12.     @Override  
  13.     public int add(int num1, int num2)  
  14.     {  
  15.         return num1 + num2;  
  16.     }  
  17.   
  18.     @Override  
  19.     public RetInfo getRetInfo(String name, int age)  
  20.     {  
  21.         RetInfo retInfo = new RetInfo();  
  22.         retInfo.setAge(age);  
  23.         retInfo.setName(name);  
  24.         return retInfo;  
  25.     }  
  26.   
  27. }  

6.返回对象

[java] view plaincopy
 
  1. package com.sysware.demo.app.model;  
  2.   
  3. public class RetInfo  
  4. {  
  5.     private String name;  
  6.     private int age;  
  7.     public String getName()  
  8.     {  
  9.         return name;  
  10.     }  
  11.     public void setName(String name)  
  12.     {  
  13.         this.name = name;  
  14.     }  
  15.     public int getAge()  
  16.     {  
  17.         return age;  
  18.     }  
  19.     public void setAge(int age)  
  20.     {  
  21.         this.age = age;  
  22.     }  
  23. }  

7.创建bean文件applicationContext.xml在WEB-INF目录下

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.     http://cxf.apache.org/jaxws   
  10.     http://cxf.apache.org/schemas/jaxws.xsd">  
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  13.     <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>   
  14.     <bean id="getInfoServiceImpl" class="com.sysware.demo.app.service.impl.GetInfoServiceImpl"></bean>  
  15.     <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint>  
  16. </beans>  

8.修改pom文件

[html] view plaincopy
 
  1. <span style="white-space:pre">  </span><modelVersion>4.0.0</modelVersion>  
  2. <span style="white-space:pre">  </span><groupId>com.sysware.demo</groupId>  
  3. <span style="white-space:pre">  </span><artifactId>mvncxfdemo</artifactId>  
  4. <span style="white-space:pre">  </span><packaging>war</packaging>  
  5. <span style="white-space:pre">  </span><version>1.0-SNAPSHOT</version>  
  6. <span style="white-space:pre">  </span><name>mvncxfdemo Maven Webapp</name>  
  7. <span style="white-space:pre">  </span><url>http://maven.apache.org</url>     
  8.         <dependencies>  
  9.         <dependency>  
  10.             <groupId>junit</groupId>  
  11.             <artifactId>junit</artifactId>  
  12.             <version>4.10</version>  
  13.             <type>jar</type>  
  14.             <scope>test</scope>  
  15.         </dependency>  
  16.         <dependency>  
  17.             <groupId>org.apache.cxf</groupId>  
  18.             <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  19.             <version>2.6.1</version>  
  20.         </dependency>  
  21.         <dependency>  
  22.             <groupId>org.springframework</groupId>  
  23.             <artifactId>spring-context</artifactId>  
  24.             <version>3.1.2.RELEASE</version>  
  25.         </dependency>  
  26.         <dependency>  
  27.             <groupId>org.springframework</groupId>  
  28.             <artifactId>spring-web</artifactId>  
  29.             <version>3.1.2.RELEASE</version>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.apache.cxf</groupId>  
  33.             <artifactId>cxf-rt-transports-common</artifactId>  
  34.             <version>2.5.4</version>  
  35.             <type>jar</type>  
  36.             <scope>compile</scope>  
  37.         </dependency>  
  38.         <dependency>  
  39.             <groupId>org.apache.cxf</groupId>  
  40.             <artifactId>cxf-rt-core</artifactId>  
  41.             <version>2.6.1</version>  
  42.             <type>jar</type>  
  43.             <scope>compile</scope>  
  44.         </dependency>  
  45.         <dependency>  
  46.             <groupId>org.apache.cxf</groupId>  
  47.             <artifactId>cxf-rt-transports-http-jetty</artifactId>  
  48.             <version>2.6.1</version>  
  49.             <type>jar</type>  
  50.             <scope>compile</scope>  
  51.         </dependency>  
  52.     </dependencies>  
  53.     <build>  
  54.         <finalName>mvncxfdemo</finalName>  
  55.         <plugins>  
  56.             <plugin>  
  57.                 <groupId>org.mortbay.jetty</groupId>  
  58.                 <artifactId>jetty-maven-plugin</artifactId>  
  59.                 <version>8.1.5.v20120716</version>  
  60.                 <configuration>  
  61.                     <stopPort>9966</stopPort>  
  62.                     <stopKey>foo</stopKey>  
  63.                 </configuration>  
  64.             </plugin>  
  65.         </plugins>  
  66.     </build>  
  67.     <properties>  
  68.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  69.     </properties>  

9.运行

[plain] view plaincopy
 
  1. mvn clean jetty:run-war  

 

10.或者实现接口修改为

[java] view plaincopy
 
  1. package com.sysware.demo.app.service.impl;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.   
  8. import com.sysware.demo.app.model.RetInfo;  
  9.   
  10. @WebService  
  11. public class GetInfoServiceImpl  
  12. {  
  13.   
  14.     @WebMethod(operationName = "add")  
  15.     @WebResult(name = "result")  
  16.     public int add(@WebParam(name = "num1"int num1,  
  17.             @WebParam(name = "num2"int num2)  
  18.     {  
  19.         return num1 + num2;  
  20.     }  
  21.   
  22.     @WebMethod(operationName = "getRetInfo")  
  23.     @WebResult(name = "result")  
  24.     public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age"int age)  
  25.     {  
  26.         RetInfo retInfo = new RetInfo();  
  27.         retInfo.setAge(age);  
  28.         retInfo.setName(name);  
  29.         return retInfo;  
  30.     }  
  31.   
  32. }  

 

由于客户端用gsoap,如果两个service在同一个目录下,将导致错误,因为两个targetNamespace相同

[html] view plaincopy
 
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://userlist.user.webservice.service.cms.ivideo.sysware.com/" elementFormDefault="unqualified" targetNamespace="http://userlist.user.webservice.service.cms.ivideo.sysware.com/" version="1.0">  


为了保证两个service不在一个target里面,建议每个package下只有一个webservice