結合spring框架來實現CXF發布SOAP協議的服務,步驟基本相同,所不同的是的多了一些配置項,步驟如下
1. 服務端
第一步:創建web項目(引入jar包)
第二步:創建SEI接口
import javax.jws.WebService; import javax.xml.ws.BindingType; import javax.xml.ws.soap.SOAPBinding; @WebService @BindingType(SOAPBinding.SOAP12HTTP_BINDING) public interface WeatherInterface { public String QueryWeather(String cityName); }
第三步:創建SEI實現類
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="com.zang.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="com.zang.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://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ws_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.jsp</welcome-file> </welcome-file-list> </web-app>
第六步:部署到tomcat下,啟動tomcat
第七步:測試服務,閱讀使用說明書 地址: http://localhost:8089/ws_cxf_spring_server/ws/weather?wsdl
如果直接創建實現類,可以使用Endpoint標簽發布服務。步驟如下
創建實現類
@WebService public class HelloWorld { public String sayHello(String name){ return "hello,"+name; } }
之前通過創建SEI接口實現時,applicationContext.xml中是用<jaxws:server>標簽來發布服務;而直接通過創建類來實現時,applicationContext.xml中應使用<jaxws:endpoint>標簽來發布服務。
<!-- <jaxws:endpoint發布SOAP協議的服務 ,對Endpoint類封裝--> <jaxws:endpoint address="/hello" implementor="com.zang.ws.cxf.server.HelloWorld"/>
重啟tomcat,訪問說明書 http://localhost:8089/ws_cxf_spring_server/ws/hello?wsdl
項目結構
2. 客戶端
第一步:引入jar包
第二步:生成客戶端代碼 wsdl2java命令,詳見客戶端實現
第三步:配置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:8089/ws_cxf_spring_server/ws/weather" serviceClass="com.zang.cxf.weather.WeatherInterface" /> </beans>
第四步:從spring上下文件獲取服務實現類,調用查詢方法,打印
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zang.cxf.weather.WeatherInterface; public class WeatheClient { 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); } }
項目結構