標籤:
昨天用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服務