整个配置过程,是我根据网上查询的一些资料,自己动手试验过的。
1 环境配置
此文档旨在讲解Apache CXF和spring整合实现在Tomcat下发布web service的实例,以供学习参考。环境信息如下:
JDK1.6.0.22
apache-tomcat-6.0.26
Spring3.2.6
apache-cxf-3.0.0
新建web项目
使用EclipseJava EE IDE for Kepler 创建一个WEB工程cxfservice
其中的WEB-INF/lib目录下的jar包为直接将apache-cxf-3.0.0.zip下载包中的apache-cxf-3.0.0\lib目录下的全部的jar,在学习过程中这种办法是最简单的了。
2.配置文件说明
(1)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/beanshttp://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>
(2)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>
(3)web.xml文件的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"version="2.5">
<display-name>cfxservice</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.发布WebService
1)要发布的HelloWorld服务的接口定义文件
packagecom.cxf.test.interfaces;
importjavax.jws.WebParam;
importjavax.jws.WebResult;
importjavax.jws.WebService;
@WebService
public interfaceHelloWorld {
/*一个简单的方法,返回一个字符串
* @param hello
* @return
* */
String say(String hello);
String sayUserName(@WebParam(name="user")UserDTOuser);
/*
* 最复杂的方法,返回一个List封装的对象集合
* */
public @WebResult(partName="o")ListObjectfindUsers();
}
2)要发布的HelloWorld服务的接口实现类
packagecom.cxf.test.interfaces;
importjava.util.ArrayList;
importjavax.jws.WebService;
/**
* webService实现类
*
* @author lenovo <br>
* @version <br>
* @CreateDate 2014年6月24日 <br>
* @see com.cxf.test.interfaces <br>
* @since <br>
*/
@WebService(endpointInterface="com.cxf.test.interfaces.HelloWorld")
publicclass HelloWorldImpl implements HelloWorld{
@Override
public String say(String hello) {
return "hello,"+ hello;
}
@Override
public String sayUserName(UserDTO user) {
return"hello,"+user.getName();
}
@Override
public ListObject findUsers() {
ArrayList<Object> list = newArrayList<Object>();
list.add(instanceUser(1,"lib"));
list.add(instanceUser(2,"mld"));
list.add(instanceUser(3,"lq"));
list.add(instanceUser(4,"gj"));
list.add(instanceUser(5,"li"));
ListObject o = new ListObject();
o.setList(list);
return o;
}
public UserDTO instanceUser(Integerid,String name){
UserDTO user = new UserDTO();
user.setId(id);
user.setName(name);
return user;
}
}
findUsers()接口返回的参数对象定义文件
3)ListObject
packagecom.cxf.test.interfaces;
importjava.util.ArrayList;
importjava.util.List;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlElement;
importjavax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="listObject",propOrder={"list"})
public classListObject {
@XmlElement(nillable=true)
protectedList<Object> list;
publicList<Object> getList(){
if(list ==null){
list = newArrayList<Object>();
}
return this.list;
}
public voidsetList(ArrayList<Object> list){
this.list = list;
}
}
UserDTO instancUser(Integer id, String name)接口返回的对象定义文件
4)UserDTO类
packagecom.cxf.test.interfaces;
importjavax.xml.bind.annotation.XmlAccessType;
importjavax.xml.bind.annotation.XmlAccessorType;
importjavax.xml.bind.annotation.XmlType;
/**
* webService传输User信息的DTO
* 分离entity类与web service接口间的耦合,隔绝entity类的修改对接口的影响.
* 使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定
* @author lenovo <br>
* @version <br>
* @CreateDate 2014年6月24日 <br>
* @see com.cxf.test.interfaces <br>
* @since <br>
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="User")
publicclass UserDTO {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
5)将WEB工程发布到Tomcat下作为一个WEB应用,webContext为cxfservice,Port为8081启动Tomcat后,以如下方式访问http://localhost:8081/cxfservice/services/helloworld?wsdl即可看到我们发布的Webservices服务HelloWorld了。在浏览器中将看到的WSDL文件另存为HelloWorld.xml即为发布的Webservice的WSDL文件。后续的调用过程与其它的操作方式完全相同。
6)客户端程序:
packagecom.cxf.test.client;
importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;
importcom.cxf.test.interfaces.HelloWorld;
importcom.cxf.test.interfaces.UserDTO;
public class HelloWorldServiceClient{
public static voidmain(String[] args) {
//调用WebService
JaxWsProxyFactoryBean factory = newJaxWsProxyFactoryBean();
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:8081/cfxservice/services/helloworld");
HelloWorld service =(HelloWorld)factory.create();
UserDTO user = newUserDTO();
user.setName("stone");
System.out.println("[result]"+service.sayUserName(user));
}
}