使用Apache cxf 和Spring在Tomcat下发布Webservice指南
转载 http://blog.iyunv.com/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包:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=1&width=18&height=18
[*]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的包了,包括如下一些
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=2&width=18&height=18
[*]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本身的包了
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=3&width=18&height=18
[*]cxf-2.2.3.jar
(2)配置文件说明
applicationContext.xml文件的内容如下:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=4&width=18&height=18
[*]
[*]
[*]
[*]
[*]
[*]
[*]
services.xml文件的内容如下:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=5&width=18&height=18
[*]
[*]
[*]
[*]
web.xml文件的内容如下:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=6&width=18&height=18
[*]
[*]
[*] cxfservice
[*]
[*] contextConfigLocation
[*] WEB-INF/classes/applicationContext.xml
[*]
[*]
[*]
[*] org.springframework.web.context.ContextLoaderListener
[*]
[*]
[*]
[*] CXFServlet
[*]
[*] org.apache.cxf.transport.servlet.CXFServlet
[*]
[*]
[*]
[*] CXFServlet
[*] /services/*
[*]
[*]
[*]
[*] index.html
[*] index.htm
[*] index.jsp
[*] default.html
[*] default.htm
[*] default.jsp
[*]
[*]
(3)发布的HelloWord服务说明
要发布的HelloWorld服务的接口定义文件com.cxf.test.interfaces.HelloWorld:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=7&width=18&height=18
[*]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:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=8&width=18&height=18
[*]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 list = new ArrayList();
[*]
[*] 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:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=9&width=18&height=18
[*]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 list;
[*]
[*] /**
[*] * Gets the value of the list property.
[*] *
[*] *
[*] * 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 set method for the list property.
[*] *
[*] *
[*] * For example, to add a new item, do as follows:
[*] *
[*] *
[*] * getList().add(newItem);
[*] *
[*] *
[*] *
[*] *
[*] * Objects of the following type(s) are allowed in the list {@link Object }
[*] *
[*] *
[*] */
[*] public List getList()
[*] {
[*] if (list == null)
[*] {
[*] list = new ArrayList();
[*] }
[*] return this.list;
[*] }
[*]
[*] public void setList(ArrayList list)
[*] {
[*] this.list = list;
[*] }
[*]
[*]}
UserDTO instancUser(Integer id, String name)接口返回的对象定义文件com.cxf.test.interfaces.UserDTO:
view plaincopy
http://static.blog.iyunv.com/scripts/ZeroClipboard/ZeroClipboard.swf?id=10&width=18&height=18
[*]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文件。后续的调用过程与其它的操作方式完全相同。
页:
[1]