XFire开发WebService实例

1、建立webservice工程,建立简单服务:

import java.util.HashMap; //Generated by MyEclipse public interface IMessage { public HashMap<String, String> example(String message); }

import java.util.HashMap; //Generated by MyEclipse public class MessageImpl implements IMessage { public HashMap<String, String> example(String message) { HashMap<String, String> map = new HashMap<String, String>(); if (message.equals("beckham")) { map.put("name", "beckham"); map.put("age", "23"); map.put("sex", "男"); map.put("add", "北京海淀"); map.put("phone", "1234567"); }else{ return null ; } return map ; } }

到此,一个简单的服务就建好了,该实例的方法由一个参数,返回map类型的数据。

发布工程,查看wsdl文件。

<?xml version="1.0" encoding="UTF-8" ?> - <wsdl:definitions targetNamespace="http://DefaultNamespace" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://DefaultNamespace" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> - <wsdl:types> - <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://DefaultNamespace"> - <xsd:element name="example"> - <xsd:complexType> - <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:element> - <xsd:complexType name="anyType2anyTypeMap"> - <xsd:sequence> - <xsd:element maxOccurs="unbounded" minOccurs="0" name="entry"> - <xsd:complexType> - <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="0" name="key" type="xsd:anyType" /> <xsd:element maxOccurs="1" minOccurs="0" name="value" type="xsd:anyType" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> - <xsd:element name="exampleResponse"> - <xsd:complexType> - <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="tns:anyType2anyTypeMap" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> - <wsdl:message name="exampleRequest"> <wsdl:part name="parameters" element="tns:example" /> </wsdl:message> - <wsdl:message name="exampleResponse"> <wsdl:part name="parameters" element="tns:exampleResponse" /> </wsdl:message> - <wsdl:portType name="MessagePortType"> - <wsdl:operation name="example"> <wsdl:input name="exampleRequest" message="tns:exampleRequest" /> <wsdl:output name="exampleResponse" message="tns:exampleResponse" /> </wsdl:operation> </wsdl:portType> - <wsdl:binding name="MessageHttpBinding" type="tns:MessagePortType"> <wsdlsoap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" /> - <wsdl:operation name="example"> <wsdlsoap:operation soapAction="" /> - <wsdl:input name="exampleRequest"> <wsdlsoap:body use="literal" /> </wsdl:input> - <wsdl:output name="exampleResponse"> <wsdlsoap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> - <wsdl:service name="Message"> - <wsdl:port name="MessageHttpPort" binding="tns:MessageHttpBinding"> <wsdlsoap:address location="http://localhost:9999/serve/services/Message" /> </wsdl:port> </wsdl:service> </wsdl:definitions>

2.建立客户端访问。

package aa; import java.net.MalformedURLException; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; 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; import defaultnamespace.AnyType2AnyTypeMap; import defaultnamespace.AnyType2AnyTypeMap.Entry; public class MessageClient { private static XFireProxyFactory proxyFactory = new XFireProxyFactory(); private HashMap endpoints = new HashMap(); private Service service0; public MessageClient() { create0(); Endpoint MessagePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://DefaultNamespace", "MessagePortTypeLocalEndpoint"), new QName( "http://DefaultNamespace", "MessagePortTypeLocalBinding"), "xfire.local://Message"); endpoints .put(new QName("http://DefaultNamespace", "MessagePortTypeLocalEndpoint"), MessagePortTypeLocalEndpointEP); Endpoint MessageHttpPortEP = service0.addEndpoint(new QName( "http://DefaultNamespace", "MessageHttpPort"), new QName( "http://DefaultNamespace", "MessageHttpBinding"), "http://localhost:9999/serve/services/Message"); endpoints.put(new QName("http://DefaultNamespace", "MessageHttpPort"), MessageHttpPortEP); } 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((aa.MessagePortType.class), props); { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://DefaultNamespace", "MessagePortTypeLocalBinding"), "urn:xfire:transport:local"); } { AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://DefaultNamespace", "MessageHttpBinding"), "http://schemas.xmlsoap.org/soap/http"); } } public MessagePortType getMessagePortTypeLocalEndpoint() { return ((MessagePortType) (this).getEndpoint(new QName( "http://DefaultNamespace", "MessagePortTypeLocalEndpoint"))); } public MessagePortType getMessagePortTypeLocalEndpoint(String url) { MessagePortType var = getMessagePortTypeLocalEndpoint(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } public MessagePortType getMessageHttpPort() { return ((MessagePortType) (this).getEndpoint(new QName( "http://DefaultNamespace", "MessageHttpPort"))); } public MessagePortType getMessageHttpPort(String url) { MessagePortType var = getMessageHttpPort(); org.codehaus.xfire.client.Client.getInstance(var).setUrl(url); return var; } @SuppressWarnings("unchecked") public static void main(String[] args) { MessageClient client = new MessageClient(); // create a default service endpoint MessagePortType service = client.getMessageHttpPort(); // 调用服务的方法 List<Entry> list = service.example("beckham").getEntry(); // 遍历数据 for (Iterator iterator = list.iterator(); iterator.hasNext();) { Entry entry = (Entry) iterator.next(); if (entry.getKey().toString().equals("name")) { System.out.println(entry.getValue()); } if (entry.getKey().toString().equals("age")) { System.out.println(entry.getValue()); } if (entry.getKey().toString().equals("sex")) { System.out.println(entry.getValue()); } if (entry.getKey().toString().equals("add")) { System.out.println(entry.getValue()); } if (entry.getKey().toString().equals("phone")) { System.out.println(entry.getValue()); } } System.exit(0); } }

运行结果:

电话:1234567 性别:男 年龄:23 姓名:beckham 地址:北京海淀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值