使用Spring整合CXF,开发基于soap协议的服务:分为服务端和客户端。
一、服务端开发
1、新建Dynamic Web Project,具体略~
2、创建服务接口
package com.xxx.cxf.server;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//soap1.2
public interface WeatherInterface {
public String query(String cityName);
}
3、创建服务实现类
package com.xxx.cxf.server;
public class WeatherInterfaceImpl implements WeatherInterface {
@Override
public String query(String cityName) {
//do samething
return "sunny";
}
}
4、发布服务
<?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">
<bean name="weatherInterface" class="com.xxx.cxf.server.WeatherInterfaceImpl"></bean>
<jaxws:server serviceClass="com.xxx.cxf.server.WeatherInterface" address="/weather">
<jaxws:serviceBean>
<ref bean="weatherInterface"/><!-- 配置服务实现类 -->
</jaxws:serviceBean>
</jaxws:server>
</beans>
5、配置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>zzzzz_cxf_spring_soap_server</display-name>
<context-param>
<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>
6、部署到Tomcat,测试服务是否发布成功,查看使用说明书
http://127.0.0.1:8080/zzzzz_cxf_spring_soap_server/ws/weather?wsdl
二、客户端开发
1、导入所需jar,略
2、使用wsdl2java工具生成客户端代码
3、配置spring
<?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 id="weatherClient" address="http://localhost:8080/zzzzz_cxf_spring_soap_server/ws/weather" serviceClass="com.xxx.cxf.client.WeatherInterface"></jaxws:client>
</beans>
4、调用方法
package com.xxx.cxf.client;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WeatherClient {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterface weatherInterface = (WeatherInterface)context.getBean("weatherClient");
String res = weatherInterface.query("a");
System.out.println(res);
}
}
OK~
最后,附上客户端工程的目录结构图