在时代不断向前发展的潮流中,我们每个人都被裹挟其中,不断被推着向前进。这也促使越来越多的人开始努力学习更多的技能,以适应时代的发展。今天主要为大家介绍,如何在java中用xfire调用webservice接口,并通过实际的操作为大家展示。
首先,这里使用的是myeclipse集成的xfire进行测试。利用xfire开发WebService,主要有三种方法:
⑴、一种是从javabean 中生成;
⑵、一种是从wsdl文件中生成;
⑶、还有一种是自己建立webservice。
具体的操作步骤是先用myeclipse建立webservice工程, 然后建立webservice接口,代码展示如下:package com.myeclipse.wsExample;
//Generated by MyEclipse
public interface IHelloWorldService
{
public String example(String message);
}
然后实现这个接口:package com.myeclipse.wsExample;
//Generated by MyEclipse
public class HelloWorldServiceImpl implements IHelloWorldService
{
public String example(String message)
{
return message;
}
}
修改service.xml 文件,加入下面的代码:
HelloWorldService
com.myeclipse.wsExample.IHelloWorldService
com.myeclipse.wsExample.HelloWorldServiceImpl
literal
application
客户端实现如下:package com.myeclipse.wsExample.client;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import com.myeclipse.wsExample.IHelloWorldService;
public class HelloWorldClient
{
public static void main(String[] args) throws MalformedURLException, Exception
{
// TODO Auto-generated method stub
Service s = new ObjectServiceFactory()
.create(IHelloWorldService.class);
XFireProxyFactory xf = new XFireProxyFactory(XFireFactory.newInstance()
.getXFire());
String url = "http://localhost:8989/HelloWorld/services/HelloWorldService";
try
{
IHelloWorldService hs = (IHelloWorldService) xf.create(s, url);
String st = hs.example("zhangjin");
System.out.print(st);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
以上就是关于在java中怎么用xfire调用webservice接口,并且通过具体的实例为大家展示出来。如果大家对java知识感兴趣,想要了解更多java程序代码例子和常见问题,敬请关注奇Q工具网。
推荐阅读: