xfire客户端对返回list很挑剔,所以需要使用泛型。

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

     如何建立webservice client

     步骤1,建立webservice  project

   

    下一步

   

    下一步,默认

   

    选择xfire

   

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

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

    在项目中右键,new webservice client

   

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

   

   完成。

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

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

  

Java代码 复制代码
  1. package com.seavision.huayi2.service;   
  2.   
  3. import java.net.MalformedURLException;   
  4. import java.util.Collection;   
  5. import java.util.HashMap;   
  6. import java.util.List;   
  7.   
  8. import javax.xml.bind.JAXBElement;   
  9. import javax.xml.namespace.QName;   
  10. import org.codehaus.xfire.XFireRuntimeException;   
  11. import org.codehaus.xfire.aegis.AegisBindingProvider;   
  12. import org.codehaus.xfire.annotations.AnnotationServiceFactory;   
  13. import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;   
  14. import org.codehaus.xfire.client.XFireProxyFactory;   
  15. import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;   
  16. import org.codehaus.xfire.service.Endpoint;   
  17. import org.codehaus.xfire.service.Service;   
  18. import org.codehaus.xfire.soap.AbstractSoapBinding;   
  19. import org.codehaus.xfire.transport.TransportManager;   
  20.   
  21. import com.seavision.huayi2.domain.ArrayOfTBusinsessLog;   
  22. import com.seavision.huayi2.domain.TBusinsessLog;   
  23. import com.seavision.huayi2.domain.TStationMonthReport;   
  24.   
  25. public class IWebserviceClient {   
  26.   
  27.     private static XFireProxyFactory proxyFactory = new XFireProxyFactory();   
  28.     private HashMap endpoints = new HashMap();   
  29.     private Service service0;   
  30.   
  31.     public IWebserviceClient() {   
  32.         create0();   
  33.         Endpoint IWebservicePortTypeLocalEndpointEP = service0 .addEndpoint(new QName("http://service.huayi2.seavision.com""IWebservicePortTypeLocalEndpoint"), new QName("http://service.huayi2.seavision.com""IWebservicePortTypeLocalBinding"), "xfire.local://IWebservice");   
  34.         endpoints.put(new QName("http://service.huayi2.seavision.com""IWebservicePortTypeLocalEndpoint"), IWebservicePortTypeLocalEndpointEP);   
  35.         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");   
  36.         endpoints.put(new QName("http://service.huayi2.seavision.com""IWebserviceHttpPort"), IWebserviceHttpPortEP);   
  37.     }   
  38.   
  39.     public Object getEndpoint(Endpoint endpoint) {   
  40.         try {   
  41.             return proxyFactory.create((endpoint).getBinding(), (endpoint).getUrl());   
  42.         } catch (MalformedURLException e) {   
  43.             throw new XFireRuntimeException("Invalid URL", e);   
  44.         }   
  45.     }   
  46.   
  47.     public Object getEndpoint(QName name) {   
  48.         Endpoint endpoint = ((Endpoint) endpoints.get((name)));   
  49.         if ((endpoint) == null) {   
  50.             throw new IllegalStateException("No such endpoint!");   
  51.         }   
  52.         return getEndpoint((endpoint));   
  53.     }   
  54.   
  55.     public Collection getEndpoints() {   
  56.         return endpoints.values();   
  57.     }   
  58.   
  59.     private void create0() {   
  60.         TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance().getXFire().getTransportManager());   
  61.         HashMap props = new HashMap();   
  62.         props.put("annotations.allow.interface"true);   
  63.         AnnotationServiceFactory asf = new AnnotationServiceFactory(new Jsr181WebAnnotations(), tm, new AegisBindingProvider(new JaxbTypeRegistry()));   
  64.         asf.setBindingCreationEnabled(false);   
  65.         service0 = asf.create((com.seavision.huayi2.service.IWebservicePortType.class), props);   
  66.         {   
  67.             AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com""IWebservicePortTypeLocalBinding"), "urn:xfire:transport:local");   
  68.         }   
  69.         {   
  70.             AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0, new QName("http://service.huayi2.seavision.com""IWebserviceHttpBinding"), "http://schemas.xmlsoap.org/soap/http");   
  71.         }   
  72.     }   
  73.   
  74.     public IWebservicePortType getIWebservicePortTypeLocalEndpoint() {   
  75.         return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com""IWebservicePortTypeLocalEndpoint")));   
  76.     }   
  77.   
  78.     public IWebservicePortType getIWebservicePortTypeLocalEndpoint(String url) {   
  79.         IWebservicePortType var = getIWebservicePortTypeLocalEndpoint();   
  80.         org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);   
  81.         return var;   
  82.     }   
  83.   
  84.     public IWebservicePortType getIWebserviceHttpPort() {   
  85.         return ((IWebservicePortType)(this).getEndpoint(new QName("http://service.huayi2.seavision.com""IWebserviceHttpPort")));   
  86.     }   
  87.   
  88.     public IWebservicePortType getIWebserviceHttpPort(String url) {   
  89.         IWebservicePortType var = getIWebserviceHttpPort();   
  90.         org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);   
  91.         return var;   
  92.     }   
  93.   
  94.     public static void main(String[] args) {   
  95.            
  96.   
  97.         IWebserviceClient client = new IWebserviceClient();   
  98.            
  99.         //create a default service endpoint   
  100.         IWebservicePortType service = client.getIWebserviceHttpPort();   
  101.           
  102.         /**  
  103.          * 使用ObjectFactory插入  
  104.          * */       
  105.             com.seavision.huayi2.domain.ObjectFactory cfactory=new com.seavision.huayi2.domain.ObjectFactory();   
  106.             com.seavision.huayi2.domain.TStationMonthReport  newTStationMonthReport = cfactory.createTStationMonthReport();   
  107.             newTStationMonthReport.setStationMonthReportId(cfactory.createTStationMonthReportStationMonthReportId("123465464864"));   
  108. //               
  109.     /**  
  110.      * 使用JAXBElement插入  
  111.      * */  
  112.                
  113. //            JAXBElement<String> id = new JAXBElement<String>(new   
  114. //                    QName("http://domain.huayi2.seavision.com", "stationMonthReportId"),String.class,"gefa");   
  115. //            newTStationMonthReport.setStationMonthReportId(id);   
  116.                
  117.             service.insertYueJiHua(newTStationMonthReport);   
  118.         /**  
  119.          * 测试返回list结果  
  120.          * */  
  121.         ArrayOfTBusinsessLog aot = service.getYeWuList();   
  122.         List list = aot.getTBusinsessLog();   
  123.         System.out.println(list.size());   
  124.         for(int i=0;i<list.size();i++){   
  125.             TBusinsessLog tlog = (TBusinsessLog) list.get(i);   
  126.             System.out.println(i+"++++++"+tlog.getLogContent().getValue());   
  127.         }   
  128.         //TODO: Add custom client code here   
  129.                 //   
  130.                 //service.yourServiceOperationHere();   
  131.            
  132.         System.out.println("test client completed");   
  133.                 System.exit(0);   
  134.     }   
  135.   
  136. }  
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进行封装后,放入对象。

  • 3dd957c0-06cc-377f-b8cd-c7119cce07d5-thumb
  • 大小: 41.4 KB
  • A7a9a430-8186-3277-ac39-70905566acf4-thumb
  • 大小: 46.1 KB
  • Be82bc5c-615a-39c0-b3ab-fdf24fa90820-thumb
  • 大小: 44.1 KB
  • 0ea2eaf7-5765-3e4d-843a-b1913c8a2b2d-thumb
  • 大小: 47 KB
  • Ce11f88b-9f25-3d3a-abb3-d0875db1fef0-thumb
  • 大小: 36.7 KB
  • 6179b30d-5f11-3506-a27f-0d938e84b908-thumb
  • 大小: 48.6 KB
原文地址:https://www.cnblogs.com/cy163/p/1418153.html