用cxf公布和调用web service
生活随笔
收集整理的這篇文章主要介紹了
用cxf公布和调用web service
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用cxf發布和調用web service
最近我們的系統需要和一個第三方系統對接,對接的方式是通過web service,所以就學習了一下這方面的東西
用CXF來做web service是比較簡單的,本文就簡單介紹一下如何一步步發布web service,以及調用現有的web service。另外如果系統已經使用了Spring MVC,那么引入CXF需要額外的步驟,見本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920。如果展現層沒有用spring mvc,而是用struts2之類的,可以省去這一步
首先介紹怎么發布web service:
第1步是創建一個普通的java接口,然后用@WebService注解聲明這是一個web service
package com.huawei.framework.webservice;import javax.jws.WebService;import com.huawei.framework.model.User;@WebService public interface HelloWorld {String sayHi(User user);}
可以看到,這個接口非常普通,不需要繼承什么額外的接口,只需要注解一個@WebService就可以
第2步當然是需要給這個接口提供一個實現類
package com.huawei.framework.webservice;import com.huawei.framework.model.User;public class HelloWorldImpl implements HelloWorld {public String sayHi(User theUser) {return "Hello " + theUser.getName() + " ,your age is "+ theUser.getAge();}}
這個類更加簡單,連注解都省了,但是如果這個類實現了不止一個接口,那么就需要加上@WebService注解,不過一般不會這樣
第3步就到了cxf出場的時候了,需要在spring配置文件中加上cxf的配置
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://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-servlet.xml" /><jaxws:endpoint id="helloWorld"implementorClass="com.huawei.framework.webservice.HelloWorldImpl"address="/HelloWorld" /></beans>
這里只寫了CXF所需的配置,spring配置文件的其他內容已省略。用到的元素是<jaxws:endpoint>,implementorClass屬性就是我們提供的實現類,然后address屬性是這個web service對外暴露的路徑
最后第4步是在web.xml中加載cxf
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="MyFramework" version="3.0"><display-name>MyFramework</display-name><context-param><param-name>contextConfigLocation</param-name><param-value> WEB-INF/beans.xml, WEB-INF/cxf.xml</param-value> </context-param><listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet><servlet-name>framework</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>framework</servlet-name><url-pattern>/</url-pattern></servlet-mapping><servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>
這里寫的比較復雜,是為了兼容spring mvc。這里把/webservice/*作為對外暴露的路徑,和上面的web service address要結合起來,比如說http://ip:port/app_name/webservice/HelloWorld,就會指向這個例子里聲明的web service接口
通過以上4步,就發布了一個web service,在瀏覽器里輸入http://ip:port/app_name/webservice,就會看到這個sayHi的web service了,并且cxf已經自動生成了wsdl文件
然后介紹一下怎么通過cxf調用已有的web service
第1步是需要“獲得”web service的對應接口類,這里的“獲得”有多種情況。在條件允許的情況下,比如2個子系統都是自己項目組開發的,或者對方愿意提供,那么直接拷貝過來就可以了。但一般是沒有這么方便的。。那么這種情況下,就是wsdl發揮作用的時候了。只要對方發布了web service,就一定會有一個wsdl文件,那么可以根據這個wsdl文件來手寫還原java接口類和所需的實體類(比如本例中的User模型類),還有一個辦法就是用wsdl2java工具,生成所需的類
在這個例子中,我直接把HelloWorld.java拷貝過來就行了,注意拷貝過來的時候,@WebService注解還是需要的
第2步是配置cxf,這里用到的是<jaxws:client>元素,和上面的<jaxws:endpoint>是對應的
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:webservice_address.properties</value></list></property></bean><jaxws:client id="helloClient" serviceClass="com.huawei.webservice.client.HelloWorld"address="${helloworld}" /></beans>
其中serviceClass屬性,就是指向HelloWorld所在的位置,address是目標web service的地址,也就是http://ip:port/app_name/webservice/HelloWorld
這里有一個額外的東西,就是把所有的address,放在單獨的webservice_address.properties文件里管理,這樣如果用到的web service比較多時,可以集中維護,不需要修改spring配置文件
第3步就是執行,十分簡單
public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("cxf.xml");HelloWorld client = (HelloWorld) context.getBean("helloClient");User user = new User();user.setName("zhengshengdong");String message = client.sayHi(user);System.out.println(message);}}
上面可以看到,首先是啟動Spring容器,然后像獲取普通的bean一樣,用getBean()方法實例化HelloWorld接口,然后直接在其上調用sayHi()方法即可
在這個過程中,CXF對底層的復雜操作進行了封裝,包括將實體數據封裝成soap格式的消息,然后塞到http的request post里,發送到目標web service。然后從返回的http response中取出soap格式的消息,再反向解析,生成實體對象
最近我們的系統需要和一個第三方系統對接,對接的方式是通過web service,所以就學習了一下這方面的東西
用CXF來做web service是比較簡單的,本文就簡單介紹一下如何一步步發布web service,以及調用現有的web service。另外如果系統已經使用了Spring MVC,那么引入CXF需要額外的步驟,見本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920。如果展現層沒有用spring mvc,而是用struts2之類的,可以省去這一步
首先介紹怎么發布web service:
第1步是創建一個普通的java接口,然后用@WebService注解聲明這是一個web service
package com.huawei.framework.webservice;import javax.jws.WebService;import com.huawei.framework.model.User;@WebService public interface HelloWorld {String sayHi(User user);}
可以看到,這個接口非常普通,不需要繼承什么額外的接口,只需要注解一個@WebService就可以
第2步當然是需要給這個接口提供一個實現類
package com.huawei.framework.webservice;import com.huawei.framework.model.User;public class HelloWorldImpl implements HelloWorld {public String sayHi(User theUser) {return "Hello " + theUser.getName() + " ,your age is "+ theUser.getAge();}}
這個類更加簡單,連注解都省了,但是如果這個類實現了不止一個接口,那么就需要加上@WebService注解,不過一般不會這樣
第3步就到了cxf出場的時候了,需要在spring配置文件中加上cxf的配置
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://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-servlet.xml" /><jaxws:endpoint id="helloWorld"implementorClass="com.huawei.framework.webservice.HelloWorldImpl"address="/HelloWorld" /></beans>
這里只寫了CXF所需的配置,spring配置文件的其他內容已省略。用到的元素是<jaxws:endpoint>,implementorClass屬性就是我們提供的實現類,然后address屬性是這個web service對外暴露的路徑
最后第4步是在web.xml中加載cxf
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="MyFramework" version="3.0"><display-name>MyFramework</display-name><context-param><param-name>contextConfigLocation</param-name><param-value> WEB-INF/beans.xml, WEB-INF/cxf.xml</param-value> </context-param><listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet><servlet-name>framework</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>framework</servlet-name><url-pattern>/</url-pattern></servlet-mapping><servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app>
這里寫的比較復雜,是為了兼容spring mvc。這里把/webservice/*作為對外暴露的路徑,和上面的web service address要結合起來,比如說http://ip:port/app_name/webservice/HelloWorld,就會指向這個例子里聲明的web service接口
通過以上4步,就發布了一個web service,在瀏覽器里輸入http://ip:port/app_name/webservice,就會看到這個sayHi的web service了,并且cxf已經自動生成了wsdl文件
然后介紹一下怎么通過cxf調用已有的web service
第1步是需要“獲得”web service的對應接口類,這里的“獲得”有多種情況。在條件允許的情況下,比如2個子系統都是自己項目組開發的,或者對方愿意提供,那么直接拷貝過來就可以了。但一般是沒有這么方便的。。那么這種情況下,就是wsdl發揮作用的時候了。只要對方發布了web service,就一定會有一個wsdl文件,那么可以根據這個wsdl文件來手寫還原java接口類和所需的實體類(比如本例中的User模型類),還有一個辦法就是用wsdl2java工具,生成所需的類
在這個例子中,我直接把HelloWorld.java拷貝過來就行了,注意拷貝過來的時候,@WebService注解還是需要的
第2步是配置cxf,這里用到的是<jaxws:client>元素,和上面的<jaxws:endpoint>是對應的
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:webservice_address.properties</value></list></property></bean><jaxws:client id="helloClient" serviceClass="com.huawei.webservice.client.HelloWorld"address="${helloworld}" /></beans>
其中serviceClass屬性,就是指向HelloWorld所在的位置,address是目標web service的地址,也就是http://ip:port/app_name/webservice/HelloWorld
這里有一個額外的東西,就是把所有的address,放在單獨的webservice_address.properties文件里管理,這樣如果用到的web service比較多時,可以集中維護,不需要修改spring配置文件
第3步就是執行,十分簡單
public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("cxf.xml");HelloWorld client = (HelloWorld) context.getBean("helloClient");User user = new User();user.setName("zhengshengdong");String message = client.sayHi(user);System.out.println(message);}}
上面可以看到,首先是啟動Spring容器,然后像獲取普通的bean一樣,用getBean()方法實例化HelloWorld接口,然后直接在其上調用sayHi()方法即可
在這個過程中,CXF對底層的復雜操作進行了封裝,包括將實體數據封裝成soap格式的消息,然后塞到http的request post里,發送到目標web service。然后從返回的http response中取出soap格式的消息,再反向解析,生成實體對象
總結
以上是生活随笔為你收集整理的用cxf公布和调用web service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 左脸部肿会是什么原因(脸部有点肿是什么原
- 下一篇: 文科生可以报哪些医学院校(文科生可以报哪