最近学习了如何使用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包:
再就是Spring的包了,包括如下一些
最后就是apache cxf本身的包了
(2)配置文件说明
applicationContext.xml文件的内容如下:
services.xml文件的内容如下:
web.xml文件的内容如下:
<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:
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:
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:
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:
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文件。后续的调用过程与其它的操作方式完全相同。