一個CXF整合SPRING的WEBSERVICE完整例項
阿新 • • 發佈:2018-12-19
1 首先準備以下JAR包
activation.jarcommons-logging-1.1.1.jarcxf-2.5.6.jarjaxb-api-2.2.1.jarjaxb-impl-2.1.3.jarjaxws-api-2.2.8.jarneethi-3.0.2.jarow2-jws-2.0-spec-1.0.11.jarspring-aop-3.0.5.RELEASE.jarspring-asm-3.0.5.RELEASE.jarspring-beans-3.0.5.RELEASE.jarspring-context-3.0.5.RELEASE.jarspring-context-support-3.0.5.RELEASE.jarspring-core-3.0 .5.RELEASE.jarspring-expression-3.0.5.RELEASE.jarspring-oxm-3.0.5.RELEASE.jarspring-web-3.0.5.RELEASE.jarspring-ws-core-2.0.2.RELEASE.jarspring-ws-security-2.0.2.RELEASE.jarspring-xml-1.5.9.jarstax-api-1.0.jarstax2-api-3.1.1.jarwebservices-api-2.2.0-1.jarwebservices-extra-2.1-20.jarwoodstox-core-asl-4.1.2.jarws-test.jarwsdl4j-1.6 .2.jarwss4j-1.6.7.jarxmlschema-core-2.0.3.jar
我的環境是IBMJDK5,因而有一些xml及webservice包是不存在的,就會比在SUN JDK的環境中要多一些JAR包,如ow2-jws-2.0-spec-1.0.11.jar,webservices-api-2.2.0-1.jar,jaxws-api-2.2.8.jar等,根據實際情況進行選擇了。
2 編寫服務端 2.1 新建一個空的WEB工程,我這裡命名為ws_test,將其放置於TOMCAT的webapps目錄; 2.2 然後在ws_test下面建立WEB-INF資料夾,並在WEB-INF資料夾中建立目錄lib; 2.3 將上面提到的JAR包都拷貝到lib目錄中; 2.4 在WEB-INF目錄中新建web.xml檔案,並將如下內容放於其中:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <description> Web Service Test. </description> <display-name>Web Service Test</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-server.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping></web-app>
這個配置檔案是WEB應用的核心配置檔案,如Listener的配置,servlet請求的對映等,這裡並配置了啟動的時候載入SPRING,並初使化我們的WEBSERVICE服務端配置檔案。2.5 在ECLIPSE中新建JAVA工程,並在工程中新建lib目錄,將上面提到的JAR包全部放到lib目錄,並將他們全部加入到classpath中; 2.6 增加服務端的JAVA類:package com.use.test.ws.entity;public class User { int id ; String name = null; String address = null; String email = null; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package com.use.test.ws.server; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import com.use.test.ws.entity.User; /** * <b>function:</b>定製客戶端請求WebService所需要的介面 */ @WebService @SOAPBinding(style = Style.RPC) public interface IService { public User getUserByName(@WebParam(name = "name") String name); public void setUser(User user);}
package com.use.test.ws.server; import java.util.Date;import javax.jws.WebParam;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.jws.soap.SOAPBinding.Style;import com.use.test.ws.entity.User; /** * <b>function:</b> WebService傳遞複雜物件,如JavaBean、Array、List、Map等 */@WebService@SOAPBinding(style = Style.RPC)@SuppressWarnings("deprecation")public class TestService implements IService { public User getUserByName(@WebParam(name = "name") String name) { User user = new User(); user.setId(new Date().getSeconds()); user.setName(name); user.setAddress("china"); user.setEmail(name + "@test.com"); return user; } public void setUser(User user) { System.out.println("############Server setUser###########"); System.out.println("setUser:" + user); }}
2.7 在src目錄下建立applicationContext-client.xml,並放入以下內容:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <bean id="userServiceBean" class="com.use.test.ws.server.TestService"/> <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <jaxws:server id="userService" serviceClass="com.use.test.ws.server.IService" address="/Users"> <jaxws:serviceBean> <ref bean="userServiceBean"/> </jaxws:serviceBean> <jaxws:inInterceptors> <ref bean="inMessageInterceptor"/> </jaxws:inInterceptors> <jaxws:outInterceptors> <ref bean="outLoggingInterceptor"/> </jaxws:outInterceptors> </jaxws:server></beans>
2.8 將建立的JAVA工程匯出jar包,只匯出src下面的內容即,並將這個jar包放到上面的TOMCAT工程中的ws_test/WEB-INF/lib目錄下,並啟動TOMCAT伺服器;2.9 啟動成功後,此時訪問URL:http://localhost:8080/ws_test/Users?wsdl,應該會看到WSDL的輸出。
3 編寫客戶端 3.1 獨立的客戶端請求類
package com.use.test.ws.client;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import com.use.test.ws.entity.User;import com.use.test.ws.server.IService;public class TestClient { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(IService.class); factory.setAddress("http://localhost:8080/ws_test/Users"); IService service = (IService) factory.create(); System.out.println("#############Client getUserByName##############"); User user = service.getUserByName("hoojo"); System.out.println(user); user.setAddress("China-Guangzhou"); service.setUser(user); }}
點選右鍵就可以執行這個類。 3.2 與SPRING的整合,首先需要在src下面建立配置檔案applicationContext-client.xml,將輸入以下內容:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:client id="userWsClient" serviceClass="com.use.test.ws.server.IService" address="http://localhost:8080/ws_test/Users"/></beans>
3.3 建立JAVA類package com.use.test.ws.client; import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.use.test.ws.entity.User;import com.use.test.ws.server.IService; /** * <b>function:</b>請求Spring整合CXF的WebService客戶端 */ public class SpringUsersWsClient { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml"); IService service = ctx.getBean("userWsClient", IService.class); System.out.println("#############Client getUserByName##############"); User user = service.getUserByName("hoojo"); System.out.println(user); user.setAddress("China-Guangzhou"); service.setUser(user); }}
直接執行這個類就OK了。
4 說明 這個例項其本上是完全照搬這篇文章,http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html,但是因為拿到我本地後無法執行,因而我這裡做了一下整理。