CXF+Spring整合发布SOAP协议的服务
服务端
开发步骤:
第一步:创建web项目(引入jar包)
第二步:创建SEI接口
package cn.hcx.ws.cxf.server;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
/**
*
* <p>Title: WeatherInterface.java</p>
* <p>Description:SEI接口</p>
*/
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
第三步:创建SEI实现类
package cn.hcx.ws.cxf.server;
/**
*
* <p>Title: WeatherInterfaceImpl.java</p>
* <p>Description:SEI实现类</p>
*/
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String queryWeather(String cityName) {
System.out.println("from client..."+cityName);
if("北京".equals(cityName)){
return "冷且霾";
} else {
return "暖且晴";
}
}
}
第四步:配置spring配置文件,applicationContext.xml,
用<jaxws:server标签发布服务,设置1.服务地址;2.设置服务接口;3设置服务实现类
<?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"
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类封装-->
<jaxws:server address="/weather" serviceClass="cn.hcx.ws.cxf.server.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 配置服务实现类 -->
<bean name="weatherInterface" class="cn.hcx.ws.cxf.server.WeatherInterfaceImpl"/>
</beans>
第五步:配置web.xml,配置spring配置文件地址和加载的listener,配置CXF的servlet
<?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_2_cxf_spring_server</display-name>
<!-- 设置spring的环境 -->
<context-param>
<!--contextConfigLocation是不能修改的 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
<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>
第六步:部署到tomcat下,启动tomcat
第七步:测试服务,阅读使用说明书
WSDL地址规则:http://ip:端口号/项目名称/servlet拦截路径/服务名称?wsdl
拦截器配置
配置applicationContext.xml中。
Endpoint标签发布服务
<jaxws:endpoint>标签
import javax.jws.WebService;
/**
*
* <p>Title: HelloWorld.java</p>
* <p>Description:简单类</p>
*/
@WebService
public class HelloWorld {
public String sayHello(String name){
return "hello,"+name;
}
}
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"
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:endpoint发布SOAP协议的服务 ,对Endpoint类封装-->
<jaxws:endpoint address="/hello" implementor="cn.hcx.ws.cxf.server.HelloWorld"/>
<!-- <jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
<jaxws:server address="/weather" serviceClass="cn.hcx.ws.cxf.server.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean>
<!-- 配置拦截器 -->
<jaxws:inInterceptors>
<ref bean="inIntercepter"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outIntercepter"/>
</jaxws:outInterceptors>
</jaxws:server>
<!-- 配置拦截器的bean -->
<bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
<bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 配置服务实现类 -->
<bean name="weatherInterface" class="cn.hcx.ws.cxf.server.WeatherInterfaceImpl"/>
</beans>
客户端
开发步骤:
第一步:引入jar包
第二步:生成客户端代码
第三步:配置spring配置文件,applicationContent.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"
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/ws_2_cxf_spring_server/ws/weather" serviceClass="cn.hcx.cxf.weather.WeatherInterface"/>
</beans>
第四步:从spring上下文中获取服务实现类
第五步:调用查询方法,打印
package cn.hcx.cxf.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.hcx.cxf.weather.WeatherInterface;
public class WeatherClient {
public static void main(String[] args) {
//初始化spring的上下文
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient");
String weather = weatherInterface.queryWeather("保定");
System.out.println(weather);
}
}