java程式調用xfire發布的webService服務

來源:互聯網
上載者:User

標籤:

昨天用xfire搭好了一個簡單的webService的服務,可以在瀏覽器訪問,今天便想要嘗試以下如何調用這個服務及相關的方法。在網上尋找了一些資料後,實現過程如下。
1、建立一個maven web項目,並建立一個帶有main方法的類。
2、匯入xfire依賴的一些jar包,為了簡單起見,我就把搭建服務端時的jar包都考了過來,放在lib檔案夾下,然後如上一篇搭建時一樣把jar加入到build path中。               
3、建立一個和服務端一樣的介面類,必須要有這個介面類才可以:
package test;public interface HelloService {    public String Hello();}

4、在main方法中調用服務介面:
package test;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;public class CallWebServiceTest {    public static void main(String[] args) {        Service srModel = new ObjectServiceFactory().create(HelloService.class);        XFireProxyFactory factory = new XFireProxyFactory(XFireFactory                .newInstance().getXFire());// 建立工廠執行個體        String helloURL = "http://localhost:8082/xfireTest/services/HelloWorld";        try {            HelloService service = (HelloService) factory.create(srModel,                    helloURL);            System.out.println("service:" + service.Hello());        } catch (Exception e) {            throw new RuntimeException(e);        }    }}


5、啟動服務端,然後啟動main方法。按正常情況下,因為服務中所寫的Hello方法返回的是“Hello”字串,因此這裡在控制台應該列印出“service:Hello”。而實際上我啟動main方法時控制台報錯,如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/methods/RequestEntity    at java.lang.Class.getDeclaredConstructors0(Native Method)    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)    at java.lang.Class.getConstructor0(Unknown Source)    at java.lang.Class.getConstructor(Unknown Source)    at org.codehaus.xfire.transport.http.HttpChannel.sendViaClient(HttpChannel.java:108)    at org.codehaus.xfire.transport.http.HttpChannel.send(HttpChannel.java:48)    at org.codehaus.xfire.handler.OutMessageSender.invoke(OutMessageSender.java:26)    at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)    at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:79)    at org.codehaus.xfire.client.Invocation.invoke(Invocation.java:114)    at org.codehaus.xfire.client.Client.invoke(Client.java:336)    at org.codehaus.xfire.client.XFireProxy.handleRequest(XFireProxy.java:77)    at org.codehaus.xfire.client.XFireProxy.invoke(XFireProxy.java:57)    at com.sun.proxy.$Proxy0.Hello(Unknown Source)    at test.CallWebServiceTest.main(CallWebServiceTest.java:19)Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.methods.RequestEntity    at java.net.URLClassLoader$1.run(Unknown Source)    at java.net.URLClassLoader$1.run(Unknown Source)    at java.security.AccessController.doPrivileged(Native Method)    at java.net.URLClassLoader.findClass(Unknown Source)    at java.lang.ClassLoader.loadClass(Unknown Source)    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)    at java.lang.ClassLoader.loadClass(Unknown Source)    ... 15 more

這意思好像是說httpclient有什麼問題,在網上查了一下,有說是少了這個包,但實際上可以看到這個包我明明是匯入了的,於是我嘗試把lib中和build path中的這個包刪除,然後再使用maven匯入,
<dependency>        <groupId>commons-httpclient</groupId>        <artifactId>commons-httpclient</artifactId>        <version>3.1</version></dependency>


然後再次啟動main方法,控制台正常輸出,

java程式調用xfire發布的webService服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.