java调用webservice例子
1. 编写实现类
a. 首先建一个接口把对外开放的方法添加进该接口,实际上就是开放此接口让客户端获得
2. 映射web.xml
通过配置web.xml来映射相应的路径,在web.xml添加如下代码
3.services配置文件
创建services文件
在src目录下创建META-INF目录,在该目录下创建一个xfire目录在该目录下创建services.xml
通过配置此文件来公开我们的接口,客户端可以通过此文件的映射可以找到相应的需要公开的接口
获取wsdl文件的地址
什么是wsdl?
参考: http://blog.csdn.net/sunchaohuang/archive/2008/10/14/3076375.aspx
通过web.xml与services.xml对地址的映射我们可以获得如下地址并进行访问后就可
以得到wsdl文件的url了
访问的地址:http://localhost:9099/HelloXfire/services/HelloXfire?wsdl
访问后会得到一个类似xml格式的内容 该文件就是一个wsdl文件
4.编写客户端
客户端获得webservice的url地址后创建对外开放的接口的实体类,然后向相应的方法传递参数,经过服务端相应的处理返回需要的结果package com.test.service; import java.net.MalformedURLException; import java.util.Collection; import java.util.HashMap; import javax.xml.namespace.QName; import org.codehaus.xfire.XFireRuntimeException; import org.codehaus.xfire.aegis.AegisBindingProvider; import org.codehaus.xfire.annotations.AnnotationServiceFactory; import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.jaxb2.JaxbTypeRegistry; import org.codehaus.xfire.service.Endpoint; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.soap.AbstractSoapBinding; import org.codehaus.xfire.transport.TransportManager; public class HelloXfireClient { private static XFireProxyFactory proxyFactory = new XFireProxyFactory(); private HashMap endpoints = new HashMap(); private Service service0; public HelloXfireClient() { create0(); Endpoint HelloXfirePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://service.test.com", "HelloXfirePortTypeLocalEndpoint"), new QName("http://service.test.com", "HelloXfirePortTypeLocalBinding"), "xfire.local://HelloXfire"); endpoints.put(new QName("http://service.test.com", "HelloXfirePortTypeLocalEndpoint"), HelloXfirePortTypeLocalEndpointEP); Endpoint HelloXfireHttpPortEP = service0 .addEndpoint(new QName("http://service.test.com", "HelloXfireHttpPort"), new QName("http://service.test.com", "HelloXfireHttpBinding"), "http://localhost:9099/HelloXfire/services/HelloXfire"); endpoints.put(new QName("http://service.test.com", "HelloXfireHttpPort"), HelloXfireHttpPortEP); } public Object getEndpoint(Endpoint endpoint) { try { return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl()); } catch (MalformedURLException e) { throw new XFireRuntimeException("Invalid URL", e); } } public Object getEndpoint(QName name) { Endpoint endpoint = ((Endpoint) endpoints.get((name))); if ((endpoint) == null) { throw new IllegalStateException("No such endpoint!"); } return getEndpoint((endpoint)); } public Collection getEndpoints() { return endpoints.values(); } private void create0() { TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager()); HashMap props = new HashMap(); props.put("annotations.allow.interface", true); AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry())); asf.setBindingCreationEnabled(false); service0 = asf.create((com.test.service.HelloXfirePortType.class), props); { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.test.com", "HelloXfirePortTypeLocalBinding"), "urn:xfire:transport:local"); } { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.test.com", "HelloXfireHttpBinding"), "http://schemas.xmlsoap.org/soap/http"); } } public HelloXfirePortType getHelloXfirePortTypeLocalEndpoint() { return ((HelloXfirePortType)(this).getEndpoint(new QName("http://service.test.com", "HelloXfirePortTypeLocalEndpoint"))); } public HelloXfirePortType getHelloXfirePortTypeLocalEndpoint(String url) { HelloXfirePortType var = getHelloXfirePortTypeLocalEndpoint(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public HelloXfirePortType getHelloXfireHttpPort() { return ((HelloXfirePortType)(this).getEndpoint(new QName("http://service.test.com", "HelloXfireHttpPort"))); } public HelloXfirePortType getHelloXfireHttpPort(String url) { HelloXfirePortType var = getHelloXfireHttpPort(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public static void main(String[] args) { HelloXfireClient client = new HelloXfireClient(); //create a default service endpoint HelloXfirePortType service = client.getHelloXfireHttpPort(); String string= service.sayHello("hello"); System.out.println(string); //TODO: Add custom client code here // //service.yourServiceOperationHere(); System.out.println("test client completed"); System.exit(0); } }
如果能够成功运行并正确获得调用的方法返回的值说明webservice成功实现了