日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring和CXF集成来实现webservices

發布時間:2024/9/27 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring和CXF集成来实现webservices 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近在負責一個大系統的實施,經過需求分析之后,將系統分為5個子系統,我們采用SOA架構,分模塊開發。項目組中最大的一個爭議就是,子系統之間的通訊問題,大家提出了兩種方案:一、如果5個子系統最后發布為5個war包,那么相互之間就不能直接調用,而是需要通過webservices等通訊方式,那會增加一些開發工作量;二、如果5個子系統合并在一個大工程中,下面放所有的模塊,那子系統間的訪問很簡單,但是日常的開發管理會存在比較大的風險。

從管理的角度來看,我是比較偏向第一種方案,因為這樣結構更清晰更簡單,開發人員之間的相互影響也較小,還有一個好處就是,可以將不同的子系統發布在不同的應用服務器上,避免了由于某一個子系統崩潰導致整個系統的崩潰。于是,我對webservices的開發技術進行了研究,發現用spring和CXF集成來發布webservice和調用webservice都非常的簡單,也加大了我選擇第一種方案的決心。下面就簡單介紹一個spring和CXF集成的示例。

一、?準備工作。
??????? 1、下載apache-cxf的應用包,地址:http://cxf.apache.org/download.html,我選擇的是2.4.1版本。

二、發布webservices
1
?新建web?project ,并加入apache-cxf-2.4.1\lib所有包,編寫要發布的web service?接口和實現.這一步,與前面一樣。

1)創建一個接口類,并加上webservice標記

?????? import javax.jws.WebService;

@WebService?

public interface HelloWorld {??

???? public String sayHello(String text);??

}

2)創建上面這個接口的實現類
?????? import javax.jws.WebService;??

@WebService(endpointInterface="test.HelloWorld")??

public class HelloWorldImpl implements HelloWorld {??

????? public String sayHello(String text) {??

????????????????? return "Hello" + text ;??

??? }??

? }?

@WebService?注解表示是要發布的web?服務,endpointInterface的值是該服務類對應的接口。

2.?spring-cxf.xml配置發布的web service?

<?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/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="hello"?class="test.HelloWorldImpl"?/>?

????<jaxws:endpoint?id="helloWorld"?implementor="#hello"?

????????address="/HelloWorld"?/>?

??</beans>

注意:<jaxws:endpoint?id="helloWorld"?implementor="#hello"?

????????address="/HelloWorld"?/>?

id:指在spring配置的beanID.

Implementor:指明具體的實現類.

Address:指明這個web service的相對地址,

3.?配置web.xml文件:

<?xml?version="1.0"?encoding="UTF-8"?>?

<web-app?version="2.4"?xmlns="http://java.sun.com/xml/ns/j2ee"?

????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?

????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee???

??? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">?

????<context-param>?

????????<param-name>contextConfigLocation</param-name>?
?????????????????? <!--spring配置文件放在WEB-INF目錄下-->

????????<param-value>/WEB-INF/spring-cxf.xml</param-value>?

????</context-param>?

????<listener>?

????????<listener-class>?

?????????org.springframework.web.context.ContextLoaderListener?

????????</listener-class>?

????</listener>?
??????????<servlet>?

????????<servlet-name>CXFServlet</servlet-name>?

????????<servlet-class>?

??????????? org.apache.cxf.transport.servlet.CXFServlet??

????????</servlet-class>?

????????<load-on-startup>1</load-on-startup>?

????</servlet>?

????<servlet-mapping>?

????????<servlet-name>CXFServlet</servlet-name>?

????????<url-pattern>/*</url-pattern>?

????</servlet-mapping>?

</web-app>?

4部署到tomcat服務器,輸入:http://localhost:8080/<web-app-name>/HelloWorld?wsdl,將顯示這個web servicewsdl.

注意:如果web.xml配置<servlet-name>CXFServlet</servlet-name>?

????????<url-pattern>/ws/*</url-pattern>?

則訪問地址為:http://localhost:8080/<web-app-name>/ws/HelloWorld?wsdl
到此webservices就發布成功了。


三、創建一個客戶端來調用webservices
1、 同樣新建 java project , 并加入 a pache-cxf-2.0.7\lib 所有包,添加到Build Path
2、將webservice的接口類導出成jar包,也添加到Build Path,主要目的是客戶端要用到服務端的HelloWorld這個類。如果不想導入這個jar包也可以,只要在客戶端創建一個一摸一樣的接口類:HelloWorld,特別要注意以下兩點:
??? 1)接口前面要添加@Webservice的標記,不然會拋出一個?javax.xml.ws.WebServiceException: Could not find wsdl:binding operation info for web method sayHelloWorld.
??? 2)包路徑也要一樣,不然會拋出一個ClassCastException: $Proxy29 cannot be cast to...
3、 配置 spring-client.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-2.0.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

??????<!-- 這個class的包路徑和類名和服務端提供web服務的接口一致-->

????<bean?id="client"?class="test.HelloWorld"

??????factory-bean="clientFactory"?factory-method="create"/>

???

????<bean?id="clientFactory"?class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
?????<!-- 這個class的包路徑和類名和服務端提供web服務的接口一致-->

??? ?<property?name="serviceClass"?value="test.HelloWorld"/>
?????<!--?這個address一定要注意,正確的-->

??? ?<property?name="address"?value="http://localhost:8080/<web-app-name>/HelloWorld"/>

????</bean>?????

</beans>

4.測試:

import?org.springframework.context.ApplicationContext;

import?org.springframework.context.support.ClassPathXmlApplicationContext;

?

import?test.HelloWorld;

?

public?class?Test {??

??? ?

????public?static?void?main(String[] args) {??

?

??????? ApplicationContext ctx =?new?ClassPathXmlApplicationContext(??

????????????????"spring-client.xml");??

??????? HelloWorld client = (HelloWorld) ctx.getBean("client");??

??????? String result = client.sayHello("Glen!");??

??????? System.out.println(result);??

??? }??

}?

結果輸出“Hello,Glen!”,測試通過,至此一個webservice的調用也成功了。

總結

以上是生活随笔為你收集整理的spring和CXF集成来实现webservices的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。