http://www.javaeye.com/topic/308452

本文介绍如何使用XFIRE搭建WebService客户端,并详细展示了如何通过客户端调用服务接口,包括使用ObjectFactory和JAXBElement进行参数封装及处理返回的List类型数据。

http://www.javaeye.com/topic/308452

服务接口,就是用来调用的,所以客户端显得尤为重要,xfire客户端对返回list很挑剔,所以需要使用泛型。

如何建立webservice client

步骤1,建立webservice project

下一步

下一步,默认

选择xfire

finish, 这样,webservice项目就完成了

接下来是如何进行客户端开发.

在项目中右键,new webservice client

接着,选择你服务文件,wsdl

完成。

这样在你的项目中,会根据wsdl服务文件,生成客户端所需要的内容。

自动生成文件,下去就细细体会,给出一个测试代码

Java代码 复制代码
  1. packagecom.seavision.huayi2.service;
  2. importjava.net.MalformedURLException;
  3. importjava.util.Collection;
  4. importjava.util.HashMap;
  5. importjava.util.List;
  6. importjavax.xml.bind.JAXBElement;
  7. importjavax.xml.namespace.QName;
  8. importorg.codehaus.xfire.XFireRuntimeException;
  9. importorg.codehaus.xfire.aegis.AegisBindingProvider;
  10. importorg.codehaus.xfire.annotations.AnnotationServiceFactory;
  11. importorg.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
  12. importorg.codehaus.xfire.client.XFireProxyFactory;
  13. importorg.codehaus.xfire.jaxb2.JaxbTypeRegistry;
  14. importorg.codehaus.xfire.service.Endpoint;
  15. importorg.codehaus.xfire.service.Service;
  16. importorg.codehaus.xfire.soap.AbstractSoapBinding;
  17. importorg.codehaus.xfire.transport.TransportManager;
  18. importcom.seavision.huayi2.domain.ArrayOfTBusinsessLog;
  19. importcom.seavision.huayi2.domain.TBusinsessLog;
  20. importcom.seavision.huayi2.domain.TStationMonthReport;
  21. publicclassIWebserviceClient{
  22. privatestaticXFireProxyFactoryproxyFactory=newXFireProxyFactory();
  23. privateHashMapendpoints=newHashMap();
  24. privateServiceservice0;
  25. publicIWebserviceClient(){
  26. create0();
  27. EndpointIWebservicePortTypeLocalEndpointEP=service0.addEndpoint(newQName("http://service.huayi2.seavision.com","IWebservicePortTypeLocalEndpoint"),newQName("http://service.huayi2.seavision.com","IWebservicePortTypeLocalBinding"),"xfire.local://IWebservice");
  28. endpoints.put(newQName("http://service.huayi2.seavision.com","IWebservicePortTypeLocalEndpoint"),IWebservicePortTypeLocalEndpointEP);
  29. EndpointIWebserviceHttpPortEP=service0.addEndpoint(newQName("http://service.huayi2.seavision.com","IWebserviceHttpPort"),newQName("http://service.huayi2.seavision.com","IWebserviceHttpBinding"),"http://localhost:8080/seavision/services/IWebservice");
  30. endpoints.put(newQName("http://service.huayi2.seavision.com","IWebserviceHttpPort"),IWebserviceHttpPortEP);
  31. }
  32. publicObjectgetEndpoint(Endpointendpoint){
  33. try{
  34. returnproxyFactory.create((endpoint).getBinding(),(endpoint).getUrl());
  35. }catch(MalformedURLExceptione){
  36. thrownewXFireRuntimeException("InvalidURL",e);
  37. }
  38. }
  39. publicObjectgetEndpoint(QNamename){
  40. Endpointendpoint=((Endpoint)endpoints.get((name)));
  41. if((endpoint)==null){
  42. thrownewIllegalStateException("Nosuchendpoint!");
  43. }
  44. returngetEndpoint((endpoint));
  45. }
  46. publicCollectiongetEndpoints(){
  47. returnendpoints.values();
  48. }
  49. privatevoidcreate0(){
  50. TransportManagertm=(org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());
  51. HashMapprops=newHashMap();
  52. props.put("annotations.allow.interface",true);
  53. AnnotationServiceFactoryasf=newAnnotationServiceFactory(newJsr181WebAnnotations(),tm,newAegisBindingProvider(newJaxbTypeRegistry()));
  54. asf.setBindingCreationEnabled(false);
  55. service0=asf.create((com.seavision.huayi2.service.IWebservicePortType.class),props);
  56. {
  57. AbstractSoapBindingsoapBinding=asf.createSoap11Binding(service0,newQName("http://service.huayi2.seavision.com","IWebservicePortTypeLocalBinding"),"urn:xfire:transport:local");
  58. }
  59. {
  60. AbstractSoapBindingsoapBinding=asf.createSoap11Binding(service0,newQName("http://service.huayi2.seavision.com","IWebserviceHttpBinding"),"http://schemas.xmlsoap.org/soap/http");
  61. }
  62. }
  63. publicIWebservicePortTypegetIWebservicePortTypeLocalEndpoint(){
  64. return((IWebservicePortType)(this).getEndpoint(newQName("http://service.huayi2.seavision.com","IWebservicePortTypeLocalEndpoint")));
  65. }
  66. publicIWebservicePortTypegetIWebservicePortTypeLocalEndpoint(Stringurl){
  67. IWebservicePortTypevar=getIWebservicePortTypeLocalEndpoint();
  68. org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
  69. returnvar;
  70. }
  71. publicIWebservicePortTypegetIWebserviceHttpPort(){
  72. return((IWebservicePortType)(this).getEndpoint(newQName("http://service.huayi2.seavision.com","IWebserviceHttpPort")));
  73. }
  74. publicIWebservicePortTypegetIWebserviceHttpPort(Stringurl){
  75. IWebservicePortTypevar=getIWebserviceHttpPort();
  76. org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
  77. returnvar;
  78. }
  79. publicstaticvoidmain(String[]args){
  80. IWebserviceClientclient=newIWebserviceClient();
  81. //createadefaultserviceendpoint
  82. IWebservicePortTypeservice=client.getIWebserviceHttpPort();
  83. /**
  84. *使用ObjectFactory插入
  85. **/
  86. com.seavision.huayi2.domain.ObjectFactorycfactory=newcom.seavision.huayi2.domain.ObjectFactory();
  87. com.seavision.huayi2.domain.TStationMonthReportnewTStationMonthReport=cfactory.createTStationMonthReport();
  88. newTStationMonthReport.setStationMonthReportId(cfactory.createTStationMonthReportStationMonthReportId("123465464864"));
  89. //
  90. /**
  91. *使用JAXBElement插入
  92. **/
  93. //JAXBElement<String>id=newJAXBElement<String>(new
  94. //QName("http://domain.huayi2.seavision.com","stationMonthReportId"),String.class,"gefa");
  95. //newTStationMonthReport.setStationMonthReportId(id);
  96. service.insertYueJiHua(newTStationMonthReport);
  97. /**
  98. *测试返回list结果
  99. **/
  100. ArrayOfTBusinsessLogaot=service.getYeWuList();
  101. Listlist=aot.getTBusinsessLog();
  102. System.out.println(list.size());
  103. for(inti=0;i<list.size();i++){
  104. TBusinsessLogtlog=(TBusinsessLog)list.get(i);
  105. System.out.println(i+"++++++"+tlog.getLogContent().getValue());
  106. }
  107. //TODO:Addcustomclientcodehere
  108. //
  109. //service.yourServiceOperationHere();
  110. System.out.println("testclientcompleted");
  111. System.exit(0);
  112. }
  113. }
package com.seavision.huayi2.service;

import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.xml.bind.JAXBElement;
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 com.seavision.huayi2.domain.ArrayOfTBusinsessLog;
import com.seavision.huayi2.domain.TBusinsessLog;
import com.seavision.huayi2.domain.TStationMonthReport;

public class IWebserviceClient {

    private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
    private HashMap endpoints = new HashMap();
    private Service service0;

    public IWebserviceClient() {
        create0();
        Endpoint IWebservicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "xfire.local://IWebservice");
        endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint"), IWebservicePortTypeLocalEndpointEP);
        Endpoint IWebserviceHttpPortEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://localhost:8080/seavision/services/IWebservice");
        endpoints.put(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort"), IWebserviceHttpPortEP);
    }

    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.seavision.huayi2.service.IWebservicePortType.class), props);
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalBinding"), "urn:xfire:transport:local");
        }
        {
            AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com", "IWebserviceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");
        }
    }

    public IWebservicePortType getIWebservicePortTypeLocalEndpoint() {
        return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebservicePortTypeLocalEndpoint")));
    }

    public IWebservicePortType getIWebservicePortTypeLocalEndpoint(String url) {
        IWebservicePortType var = getIWebservicePortTypeLocalEndpoint();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }

    public IWebservicePortType getIWebserviceHttpPort() {
        return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com", "IWebserviceHttpPort")));
    }

    public IWebservicePortType getIWebserviceHttpPort(String url) {
        IWebservicePortType var = getIWebserviceHttpPort();
        org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
        return var;
    }

    public static void main(String[] args) {
        

        IWebserviceClient client = new IWebserviceClient();
        
		//create a default service endpoint
        IWebservicePortType service = client.getIWebserviceHttpPort();
       
        /**
         * 使用ObjectFactory插入
         * */    
            com.seavision.huayi2.domain.ObjectFactory cfactory=new com.seavision.huayi2.domain.ObjectFactory();
            com.seavision.huayi2.domain.TStationMonthReport  newTStationMonthReport = cfactory.createTStationMonthReport();
            newTStationMonthReport.setStationMonthReportId(cfactory.createTStationMonthReportStationMonthReportId("123465464864"));
//            
    /**
     * 使用JAXBElement插入
     * */
            
//            JAXBElement<String> id = new JAXBElement<String>(new
//                    QName("http://domain.huayi2.seavision.com", "stationMonthReportId"),String.class,"gefa");
//            newTStationMonthReport.setStationMonthReportId(id);
            
            service.insertYueJiHua(newTStationMonthReport);
        /**
         * 测试返回list结果
         * */
        ArrayOfTBusinsessLog aot = service.getYeWuList();
        List list = aot.getTBusinsessLog();
        System.out.println(list.size());
        for(int i=0;i<list.size();i++){
        	TBusinsessLog tlog = (TBusinsessLog) list.get(i);
        	System.out.println(i+"++++++"+tlog.getLogContent().getValue());
        }
		//TODO: Add custom client code here
        		//
        		//service.yourServiceOperationHere();
        
		System.out.println("test client completed");
        		System.exit(0);
    }

}

注意以下内容,1:返回list内容,先存放入Arrayof**对象当中,再进行list遍历。

2: 对象属性取值,getValue();

3:对象属性封装,通过JAXBElement进行封装后,放入对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值