1.建项目,导包.

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn_itcast.maven</groupId>
<artifactId>cxf_ws_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cxf_ws_spring</name>
<description>CXF的WS整合Spring发布</description> <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> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- Spring整合junit开发 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9998</port>
</configuration>
</plugin>
</plugins>
</build>
</project>

pom.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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"> <!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 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>
<!-- 欢迎页面 -->
<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>

web.xml

2.导入实体类/service

 package cn.itcast.cxf.service;

 import java.util.List;

 import javax.jws.WebMethod;
import javax.jws.WebService; import cn.itcast.cxf.domain.Car;
import cn.itcast.cxf.domain.User; @WebService
public interface IUserService {
@WebMethod
public String sayHello(String name); @WebMethod
public List<Car> findCarsByUser(User user);
}

IUIservice

 package cn.itcast.cxf.service;

 import java.util.ArrayList;
import java.util.List; import javax.jws.WebService; import cn.itcast.cxf.domain.Car;
import cn.itcast.cxf.domain.User;
//设置endpointInterface接口服务完整类名,serviceName服务名称.
@WebService(endpointInterface = "cn.itcast.cxf.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;
}
}
}

UserviceImpl

 package cn.itcast.cxf.domain;

 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 + "]";
} }

Car

 package cn.itcast.cxf.domain;

 import java.util.ArrayList;
import java.util.List; 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;
} }

User

3.配置Springcxf服务发布

 <?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"> <!--
address 客户端访问服务路径
serviceClass 配置接口
serviceBean 配置实现类
-->
<jaxws:server id="userService" address="/userService"
serviceClass="cn.itcast.cxf.service.IUserService">
<jaxws:serviceBean>
<bean class="cn.itcast.cxf.service.UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server> </beans>

applicationContext.xml

访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl

4.整合Spring测试,编写客户端

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" 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.itcast.cxf.service.IUserService"
address="http://localhost:9998/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>

客户端的配置

2)测试类编写

 package cxf_ws_spring;

 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.cxf.service.IUserService; @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("我是程序员"));
}
}

测试类

最新文章

  1. jQuery学习之路(5)- 简单的表单应用
  2. 【HDU】1599 find the mincost route
  3. shell脚本连接、读写、操作mysql数据库实例
  4. WebView返回时设置Title
  5. C语言文件操作
  6. Oracle 时间,日期 类型函数及参数详解
  7. loadrunner负载测试实例
  8. Verilog HDL基础语法讲解之模块代码基本结构
  9. Sqlite3笔记
  10. CSS Sprites优点
  11. Device Tree常用方法解析
  12. ActionResult 之HttpGet HttpPost
  13. 解决MVC模式文件下载附件中文名称乱码
  14. POJ 3171 Cleaning Shifts
  15. Android应用程序如何使用Internet资源?
  16. Docker介绍及使用
  17. oracle 优化or 更换in、exists、union all几个字眼
  18. Docker 安装redis(四)
  19. 在vue项目中使用canvas-nest.js,报parameter 1 is not of type &#39;Element&#39;
  20. Locust 设置响应断言

热门文章

  1. [android] 轮播图-无限循环
  2. 比较全git的.ignore文件配置
  3. 【Java并发编程】3、DelayQueue应用场景,多考生考试
  4. Android - AsyncTask你知道多少?
  5. SVN查看所有日志提交记录
  6. python-组合模式
  7. 【代码笔记】iOS-产生随机字符串
  8. SD从零开始38-40
  9. sql server 大批数据插入时,时间过长的问题
  10. ActiveReports 报表控件V12新特性 -- 页面报表新增子报表