服务端接口:
package com.whaty.test.ws.cxf.service;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.whaty.platform.entity.user.User;
/**
* @name:IUserService.java
* @desc:
* @author:lizhuang
* @createTime:2012-12-29 上午09:40:58
*/
@WebService
public interface IUserService {
public User getUserByName(@WebParam(name = "name")String name);
public void setUser(User user);
}
实现类:
package com.whaty.test.ws.cxf.service;
import java.util.Random;
import javax.jws.WebService;
import com.whaty.platform.entity.user.User;
/**
* @name:UserService.java
* @desc:
* @author:lizhuang
* @createTime:2012-12-29 上午10:00:16
*/
@WebService
public class UserService implements IUserService {
@Override
public User getUserByName(String name) {
User user = new User();
user.setId(String.valueOf(new Random().nextInt(Integer.MAX_VALUE)));
user.setName(name);
user.setAddress("北京");
user.setEmail(name + "@tudou.com");
System.out.println(user.getId()+"服务端接收:"+name);
return user;
}
@Override
public void setUser(User user) {
System.out.println("User:" + user);
}
}
服务端的spring配置:
上面的导入部分是必须的。具体可参加apache-cxf的官网示例
<?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">
<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" />
<bean id="userServiceBean" class="com.whaty.test.ws.cxf.service.UserService" />
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="com.whaty.test.ws.cxf.service.IUserService" address="/user">
<jaxws:serviceBean><!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
然后在web.xml中配置好apache-cxf和spring监听。
<?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">
<display-name>Archetype Created Web Application</display-name>
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener><!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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>
启动jetty--->jetty:run
测试下是否成功:http://localhost:9999/whaty/user?wsdl
下面写个简单的客户端测试一下。
package com.whaty.test.ws.cxf.service.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.whaty.platform.entity.user.User;
import com.whaty.test.ws.cxf.service.IUserService;
/**
* @name:UserServiceClient.java
* @desc:
* @author:lizhuang
* @createTime:2012-12-29 上午11:03:14
*/
public class UserServiceClient {
public static void main(String[] args) { // 调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IUserService.class);
factory.setAddress("http://localhost:9999/whaty/user");
IUserService service = (IUserService) factory.create();
System.out.println("------------使用普通方法访问客户端---------------");
User user = service.getUserByName("tudou");
System.out.println(user);
user.setAddress("北京-土豆");
service.setUser(user);
}
}
控制台客户端打印:
------------使用普通方法访问客户端---------------
com.whaty.platform.entity.user.User@19c7c21
报务端打印:
1521306326服务端接收:tudou
User:com.whaty.platform.entity.user.User@c2ccac
下面我们把客户端也做成spring的。
配置客户端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" />
<jaxws:client id="userWsClient" serviceClass="com.whaty.test.ws.cxf.service.IUserService" address="http://localhost:9999/whaty/user" />
</beans>
编写测试类:
package com.whaty.test.ws.cxf.service.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.whaty.platform.entity.user.User;
import com.whaty.test.ws.cxf.service.IUserService;
/**
* @name:UserServiceCxfClient.java
* @desc:
* @author:lizhuang
* @createTime:2012-12-29 上午11:34:00
*/
public class UserServiceSpringCxfClient {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-ws-client.xml");
IUserService service = ctx.getBean("userWsClient", IUserService.class);
System.out.println("------------使用spring-bean访问客户端---------------");
User user = service.getUserByName("tudou");
System.out.println(user);
user.setAddress("湖北-土豆");
service.setUser(user);
}
}
右键运行访问服务端的webservices,控制台是一样的效果的。
源码: