spring和CXF集成来实现webservices
從管理的角度來看,我是比較偏向第一種方案,因為這樣結構更清晰更簡單,開發人員之間的相互影響也較小,還有一個好處就是,可以將不同的子系統發布在不同的應用服務器上,避免了由于某一個子系統崩潰導致整個系統的崩潰。于是,我對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配置的bean的ID.
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 service的wsdl.
注意:如果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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 坡度比1:1.5示意图(坡度比1 1 5
- 下一篇: spring+cxf调用webservi