首先,新建项目名为cxf_Spring,
拷贝所需jar包至lib目录,CXF和spring整合需要准备如下jar包文件,目录结构的截图如附件中的1.jpg文件:
web.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 加载Spring容器配置 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 设置Spring容器加载配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext**.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- CXF配置 --> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:
下面开始写服务器端代码,实体操作bean的代码如下:
package com.wxl.bean;
public class User {
private Integer id;
private String name;
private String email;
private String address;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "id:" + this.id + " name:" + this.name + " email:" + this.email
+ " address:" + this.address;
}
}
首先定制服务器端的接口,代码如下:
package com.wxl.server.service;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.wxl.bean.User;
/**
* 定制客户端请求WebService所需要的接口
*
* @author wangxl
*
*/
@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
public User getUserByName(@WebParam(name = "name")
String name);
public void setUser(User user);
}
下面编写WebService接口的实现类,代码如下:
package com.wxl.server.service.impl;
import java.util.Date;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.wxl.bean.User;
import com.wxl.server.service.IComplexUserService;
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserServiceImpl implements IComplexUserService {
public User getUserByName(String name) {
User user = new User();
user.setId(new Date().getSeconds());
user.setName(name);
user.setEmail(name + "@yahoo.cn");
user.setAddress("Chian");
return user;
}
public void setUser(User user) {
System.out.println("############Server setUser###########");
System.out.println("setUser:" + user);
}
}
注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
下面要在applicationContext-server.xml文件中添加如下配置:
<!-- 注入接口实现类 --> <bean id="userServiceBean" class="com.wxl.server.service.impl.ComplexUserServiceImpl" /> <!-- 日志拦截器 --> <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"> <constructor-arg value="receive" /> </bean> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" /> <!-- 注意下面的address,这里的address的名称就是访问的WebService的name --> <jaxws:server id="userService" serviceClass="com.wxl.server.service.IComplexUserService" address="/users"> <jaxws:serviceBean> <!-- 要暴露的 bean 的引用 --> <ref bean="userServiceBean" /> </jaxws:serviceBean> <jaxws:inInterceptors> <ref bean="inMessageInterceptor" /> </jaxws:inInterceptors> <jaxws:outInterceptors> <ref bean="outLoggingInterceptor" /> </jaxws:outInterceptors> </jaxws:server>
下面将项目部署到tomcat,启动tomcat服务器后,在浏览器中请求如下地址:
http://localhost:8088/cxf_Spring/users?wsdl
如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的users就是上面xml配置中的address的名称,是一一对应的。
下面编写客户端请求的代码,代码如下:
package com.wxl.client.test;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wxl.bean.User;
import com.wxl.server.service.IComplexUserService;
//client端java的实现
public class UserWSClient {
public static void main(String[] args) {
new UserWSClient().WSClientTest();
}
public void WSClientTest() {
// 调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IComplexUserService.class);
factory.setAddress("http://localhost:8088/cxf_Spring/users");
// 添加日志拦截器
factory.getInInterceptors().add(new LoggingInInterceptor());// 请求的
factory.getOutInterceptors().add(new LoggingOutInterceptor());// 发送出去的
IComplexUserService service = (IComplexUserService) factory.create();
System.out.println("#############JAxWs Client getUserByName##############");
User user = service.getUserByName("wxl");
System.out.println(user.toString());
user.setAddress("China-Hangzhou");
service.setUser(user);
}
}
在main函数中调用WSClientTest()方法,运行后可以在控制台中看到如下信息:
2012-2-28 16:23:12 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.server.wxl.com/}IComplexUserServiceService from class com.wxl.server.service.IComplexUserService
#############JAxWs Client getUserByName##############
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Outbound Message
---------------------------
ID: 1
Address: http://localhost:8088/cxf_Spring/users
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:getUserByName xmlns:ns1="http://service.server.wxl.com/"><name>wxl</name></ns1:getUserByName></soap:Body></soap:Envelope>
--------------------------------------
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Inbound Message
----------------------------
ID: 1
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[302], content-type=[text/xml;charset=UTF-8], Date=[Tue, 28 Feb 2012 08:23:13 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:getUserByNameResponse xmlns:ns1="http://service.server.wxl.com/"><return><address>Chian</address><email>wxl@yahoo.cn</email><id>13</id><name>wxl</name></return></ns1:getUserByNameResponse></soap:Body></soap:Envelope>
--------------------------------------
id:13 name:wxl email:wxl@yahoo.cn address:Chian
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Outbound Message
---------------------------
ID: 2
Address: http://localhost:8088/cxf_Spring/users
Encoding: UTF-8
Content-Type: text/xml
Headers: {Accept=[*/*], SOAPAction=[""]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:setUser xmlns:ns1="http://service.server.wxl.com/"><arg0><address>China-Hangzhou</address><email>wxl@yahoo.cn</email><id>13</id><name>wxl</name></arg0></ns1:setUser></soap:Body></soap:Envelope>
--------------------------------------
2012-2-28 16:23:13 org.apache.cxf.services.IComplexUserServiceService.IComplexUserServicePort.IComplexUserService
信息: Inbound Message
----------------------------
ID: 2
Response-Code: 200
Encoding: UTF-8
Content-Type: text/xml;charset=UTF-8
Headers: {Content-Length=[195], content-type=[text/xml;charset=UTF-8], Date=[Tue, 28 Feb 2012 08:23:13 GMT], Server=[Apache-Coyote/1.1]}
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns1:setUserResponse xmlns:ns1="http://service.server.wxl.com/"></ns1:setUserResponse></soap:Body></soap:Envelope>
--------------------------------------
这个server端是通过Spring整合配置的,client端是我们手动访问发布的地址来实现访问。下面我们将Client端也通过Spring配置完成整合。
首先在src下增加applicationContext-client.xml配置文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" 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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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" /> <!-- client端的spring实现 --> <jaxws:client id="userWsClient" serviceClass="com.wxl.server.service.IComplexUserService" address="http://localhost:8088/cxf_Spring/users"> </jaxws:client> </beans>
在客户端UserWSClient类中添加一个Spring实现的方法,请求代码如下:
public void springWsClientTest() {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext-client.xml");
IComplexUserService service = ctx.getBean("userWsClient",
IComplexUserService.class);
System.out
.println("#############Spring Client getUserByName##############");
User user = service.getUserByName("wxl");
System.out.println(user.toString());
user.setAddress("China-Hangzhou");
service.setUser(user);
}
在main函数中运行 springWsClientTest()方法后,结果如下:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
2012-2-28 16:25:20 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.server.wxl.com/}IComplexUserServiceService from class com.wxl.server.service.IComplexUserService
#############Spring Client getUserByName##############
id:20 name:wxl email:wxl@yahoo.cn address:Chian