一、pom.xml
<dependencies>
<!-- CXF WS核心包 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- spring 核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- spring test核心包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
二、Spring配置文件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" 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的名称空间:
xmlns:jaxws="http://cxf.apache.org/jaxws"
引入jaxws.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
-->
<!--
address 客户端访问服务路径
serviceClass 配置接口
serviceBean 配置实现类
-->
<jaxws:server id="userService" address="/userService"
serviceClass="cn.cxf.maven.service.IUserService">
<jaxws:serviceBean>
<bean class="cn.cxf.maven.service.UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
三、实体类
User
public class User {
private Integer id;
private String username;
private String city;
private List<Car> cars = new ArrayList<Car>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
}
Car
public class Car {
private Integer id;
private String carName;
private Double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";
}
}
四、接口类
IUserService
@WebService
public interface IUserService {
@WebMethod
public String sayHello(String name);
@WebMethod
public List<Car> findCarsByUser(User user);
}
IUserService实现类UserServiceImpl
@WebService(endpointInterface = "cn.cxf.maven.service.IUserService", serviceName = "userService")
public class UserServiceImpl implements IUserService {
// 简单参数传递
public String sayHello(String name) {
return "Hello," + name;
}
// 复杂参数传递
public List<Car> findCarsByUser(User user) {
if ("xiaoming".equals(user.getUsername())) {
List<Car> cars = new ArrayList<Car>();
Car car1 = new Car();
car1.setId(1);
car1.setCarName("大众途观");
car1.setPrice(200000d);
cars.add(car1);
Car car2 = new Car();
car2.setId(2);
car2.setCarName("现代ix35");
car2.setPrice(170000d);
cars.add(car2);
return cars;
} else {
return null;
}
}
}
五、web.xml
<!-- CXF基于web访问 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
六、客户端测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class JAXWS_Spring_Test {
@Autowired
private IUserService proxy;
@Test
public void testCXF() {
System.out.println(proxy.sayHello("test"));
}
}
七、客户端测试类Spring配置文件applicationContext-test.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">
<!--
serviceClass 服务接口
address 服务访问地址
-->
<jaxws:client id="userServiceClient"
serviceClass="cn.cxf.maven.service.IUserService"
address="http://localhost:6060/cxf_ws_spring/services/userService" >
<!-- 来源消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</jaxws:inInterceptors>
<!-- 输出消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>