转载 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包:
- commons-logging-1.1.1.jar
- geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
- geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
- geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
- geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
- geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
- geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
- geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
- jaxb-api-2.1.jar
- jaxb-impl-2.1.12.jar
- jetty-6.1.21.jar
- jetty-util-6.1.21.jar
- neethi-2.0.4.jar
- saaj-api-1.3.jar
- saaj-impl-1.3.2.jar
- wsdl4j-1.6.2.jar
- wstx-asl-3.2.8.jar
- XmlSchema-1.4.5.jar
- xml-resolver-1.2.jar
再就是Spring的包了,包括如下一些
- aopalliance-1.0.jar
- spring-core-2.5.5.jar
- spring-beans-2.5.5.jar
- spring-context-2.5.5.jar
- spring-web-2.5.5.jar
最后就是apache cxf本身的包了
- cxf-2.2.3.jar
(2)配置文件说明
applicationContext.xml文件的内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <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:services.xml" />
- </beans>
services.xml文件的内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <jaxws:endpoint id="webServiceHelloWorld"
- address="/HelloWorld" implementor="com.cxf.test.interfaces.HelloWorldImpl"/>
- </beans>
web.xml文件的内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>cxfservice</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/classes/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <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>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
(3)发布的HelloWord服务说明
要发布的HelloWorld服务的接口定义文件com.cxf.test.interfaces.HelloWorld:
- package com.cxf.test.interfaces;
- import javax.jws.WebParam;
- import javax.jws.WebResult;
- import javax.jws.WebService;
- @WebService
- public interface HelloWorld
- {
- /*
- * 一个简单的方法,返回一个字符串
- *
- * @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();
- }
要发布的HelloWorld服务的接口实现类com.cxf.test.interfaces.HelloWorldImpl:
- package com.cxf.test.interfaces;
- import java.util.ArrayList;
- import javax.jws.WebService;
- /**
- * @author zhangzk
- *
- */
- /**
- * WebService实现类.
- *
- * 使用@WebService指向Interface定义类即可.
- */
- @WebService(endpointInterface = "com.cxf.test.interfaces.HelloWorld")
- public class HelloWorldImpl implements HelloWorld
- {
- 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;
- }
- }
findUsers()接口返回的参数对象定义文件com.cxf.test.interfaces.ListObject:
- package com.cxf.test.interfaces;
- import java.util.ArrayList;
- import java.util.List;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlType;
- @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;
- }
- }
UserDTO instancUser(Integer id, String name)接口返回的对象定义文件com.cxf.test.interfaces.UserDTO:
- package com.cxf.test.interfaces;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlType;
- /**
- * 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;
- }
- }
(4)将WEB工程发布到Tomcat下作为一个WEB应用,webContext为cxfservice,Port为9000
启动Tomcat后,以如下方式访问http://localhost:9000/cxfservice/services/HelloWorld?wsdl即可看到我们发布的Webservices服务HelloWorld了。在浏览器中将看到的WSDL文件另存为HelloWorld.xml即为发布的Webservice的WSDL文件。后续的调用过程与其它的操作方式完全相同。