Yesterday with Xfire a simple WebService service that can be accessed in the browser, today you want to try the following how to invoke the service and related methods. After finding some information on the Internet, the implementation process is as follows.
1. Create a MAVEN Web project and create a class with the Main method.
2, import Xfire dependency of some jar package, for the sake of simplicity, I set up the service side of the jar package has been tested, placed in the Lib folder, and then as the previous build when the jar added to the build path.
3, to create a service-side interface class, you must have this interface class to:
Package Test;public interface HelloService {public String Hello ();}
4. Call the service interface in the Main method:
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 ());//Create factory instance 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. Start the server, and then start the main method. Normally, because the Hello method written in the service returns a "Hello" string, "Service:hello" should be printed in the console here. And actually I start the Main method when the console error, as follows:
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 O Rg.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. $Proxy 0.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.UR Lclassloader$1.run (Unknown Source) at java.security.AccessController.doPrivileged (Native Method) at Java.net.URLClas Sloader.findclass (Unknown source) at Java.lang.ClassLoader.loadClass (Unknown source) at Sun.misc.launcher$appclasslo Ader.loadclass (Unknown source) at Java.lang.ClassLoader.loadClass (Unknown source) ... More
This means that it seems that httpclient have any problems, on-line check, there is said to be less this package, but actually can see this package I was imported, so I tried to lib and build path in the package deleted, and then use MAVEN import,
<dependency> <groupId>commons-httpclient</groupId> <artifactId> Commons-httpclient</artifactid> <version>3.1</version></dependency>
then start the main method again, the console output normally,
Java program calls Xfire published WebService service