spring+CXF开发SOAP协议的WebService
使用apache-cxf-2.7.18版本的cxf相关jar包
一、项目结构图——服务端:
服务接口:IWeatherInterface.java
package com.zxj.weather.ws;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface IWeatherInterface {
public String getWeather(String cityName);
}
服务接口实现类:WeatherInterfaceImpl.java
package com.zxj.weather.ws.impl;
import javax.jws.WebService;
import org.springframework.stereotype.Component;
import com.zxj.weather.ws.IWeatherInterface;
@Component("weatherInterfaceImpl")
@WebService
public class WeatherInterfaceImpl implements IWeatherInterface {
@Override
public String getWeather(String cityName) {
System.out.println("来自于客户端:"+cityName);
return "晴";
}
}
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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws" 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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.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">
<!-- 自动扫描 -->
<context:component-scan base-package="com.zxj.weather.ws" />
<!-- 定义服务提供者 -->
<jaxws:endpoint address="/weather" implementor="#weatherInterfaceImpl">
<!-- 添加in拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<!-- 添加out拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:outInterceptors>
</jaxws:endpoint>
</beans>
web.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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ws_cxf_spring_server</display-name>
<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>
<!-- 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>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
二、项目结构图——客户端:
1、在dos窗口中,通过wsdl2java生成客户端代码
wsdl2java -d . -p com.zxj.weather.ws.impl http://127.0.0.1:8080/ws_cxf_spring_server/webservice/weather?wsdl
备注:
-d . 代表在当前文件夹中生成代码
-p com.zxj.weather.ws.impl 代表生成代码的包名
2、生成的客户端代码列表如下:
3、调用web服务的类:WeatherClient.java
package com.zxj.weather.ws.client;
import com.zxj.weather.ws.impl.IWeatherInterface;
import com.zxj.weather.ws.impl.WeatherInterfaceImplService;
public class WeatherClient {
public static void main(String[] args) {
WeatherInterfaceImplService weatherInterfaceImplService = new WeatherInterfaceImplService();
IWeatherInterface weatherInterface = weatherInterfaceImplService.getWeatherInterfaceImplPort();
String weather = weatherInterface.getWeather("成都");
System.out.println("成都:"+weather);
}
}