最近項目中有用到Webservice,研究了Spring與CXF整合的官方文檔(其實官方文檔說的清楚了,建議大伙還是看官方文檔,都是很簡單英文單詞),所以就有了下麵的demo,相信大伙都能看懂 用的主要架構為Maven + Spring + CXF + IDEA,廢話就不多說了,先看下整個個項目結構...
最近項目中有用到Webservice,研究了Spring與CXF整合的官方文檔(其實官方文檔說的清楚了,建議大伙還是看官方文檔,都是很簡單英文單詞),所以就有了下麵的demo,相信大伙都能看懂
用的主要架構為Maven + Spring + CXF + IDEA,廢話就不多說了,先看下整個個項目結構
一、項目結構圖
二、服務端
1.我們首先來編寫服務端介面HelloWorld.java
package cn.starlin.springcxf; import javax.jws.WebService; /** * 定義服務介面 * on 2016/03/01 9:34. */ @WebService public interface HelloWorld { public String sayHello(String str); }
2.定義服務端介面實現類HelloWorldImpl
package cn.starlin.springcxf; import javax.jws.WebService; /** * 服務介面實現類 * on 2016/03/01 9:36. */ @WebService(endpointInterface = "cn.starlin.springcxf.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHello(String str) { System.out.println("syaHello called"); return "Hello " + str; } }
3.聲明servers的bean,創建cxf-servlet.xml文件
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!--由Spring管理--> <bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> </beans>
4.配置web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>cxf</display-name> <servlet> <description>Apache CXF Endpoint</description> <display-name>cxf</display-name> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
5.啟動Tomcat,訪問http://localhost:8080/SpringCXF/,若能出現下圖界面,就表示服務端成功了
三、客戶端
1.客戶端 client-beans.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> <bean id="client" class="cn.starlin.springcxf.HelloWorld" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/> <property name="address" value="http://localhost:8080/SpringCXF/HelloWorld"/> </bean> </beans>
2.客戶端代碼HelloWorldClien.java
package cn.starlin.springcxf; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 客戶端 * on 2016/03/01 10:53. */ public class HelloWorldClient { public static void main(String args[]) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client");//client需與client-beans.xml中的id一致 String response = client.sayHello("starlin"); System.out.println("Response: " + response); System.exit(0); } }
3.運行以下客戶端代碼,出現以下信息就表示成功了
同時服務端也會列印出相關信息
至此整合完成!!!!