结合spring框架来实现CXF发布SOAP协议的服务,步骤基本相同,所不同的是的多了一些配置项,步骤如下。
1.服务器程序
- 创建一个web工程
- 导入依赖包
<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>com.webservice.demo</groupId>
<artifactId>Server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- 集中定义依赖版本号 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<junit.version>4.12</junit.version>
<spring.version>4.3.8.RELEASE</spring.version>
<cxf.version>3.2.6</cxf.version>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.6</version>
</dependency>
</dependencies>
</project>
(3)拷贝cxf开发的java工程的代码到web工程中:
(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
public class WeatherInterfaceImpl implements WeatherInterface {
public String queryWeather(String cityName) {
if ("成都".equals(cityName)) {
return "冷且霾";
} else {
return "暖且晴";
}
}
}
(4)配置Spring配置文件 applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!--jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装 -->
<!--
发布一个服务
id="aa":表示惟一标识
address:表示发布服务的项目地址,weatherServer此时就表示访问url的时候,在?wsdl之前要存在weatherServer,也就是weatherServer?wsdl
serviceClass:表示指定发布服务的接口的路径
-->
<jaxws:server serviceClass="com.webservice.demo.WeatherInterface" address="/weather">
<jaxws:serviceBean>
<ref bean="weatherInterface" />
</jaxws:serviceBean>
<!--配置拦截器 -->
<jaxws:inInterceptors>
<ref bean="inInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
<!-- 配置服务实现类 -->
<bean name="weatherInterface" class="com.webservice.demo.WeatherInterfaceImpl"></bean>
<!--配置拦截器bean -->
<bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
</beans>
(5)第五步:配置web.xml
<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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>D</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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的Servlet -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
第六步:启动tomcat,部署web工程到tomcat
第七步:测试服务是否发布成功
WSDL地址规则:http://localhost:端口号/项目名称/servlet拦截路径/服务名称?wsdl
2.客户端程序
- 第一步:创建web工程作为客户端,引入cxf的jar包
- 第二步:生成客户端代码
执行命令:wsdl2java -p 包名 -d . 服务说明书地址
示例:
wsdl2java -p com.webservice.client -d .http://localhost:8080/Server1/ws/weather?wsdl - 第三步:配置spring的配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!--jaxws:client实现客户端配置,对JaxWsProxyFactoryBean类封装 -->
<jaxws:client id="weatherClient" address="http://127.0.0.1:8080/Server1/ws/weather"
serviceClass="com.webservice.client.WeatherInterface"></jaxws:client>
</beans>
第四步:初始化spring上下文,获取接口实现类,调用查询方法
public class WeatherClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient");
System.out.println(weatherInterface.queryWeather("北京"));
}
}