昨天用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开发,现居湖北武汉,有过支付、OA、CA、OAUTH2等工作经验,现从事车联网行业。爱好分享,个人博客blog.tzxcode.cn,微信tuzongxun,qq1160569243,欢迎来撩。