使用java6做webservice 的用戶端

來源:互聯網
上載者:User

在上一篇博文介紹了如何使用java 6內建的伺服器跑wervice。那麼我們緊接著根據發行的服務產生其服務的用戶端。

我們只需要使用wsimport來產生用戶端代碼。

在命令列中輸入  :wsimport -keep -p com.geostar.geoglobe.jaxws.client  http://126.33.8.183:9999/GeoGlobeServer?wsdl

點擊斷行符號,立馬會為我們產生用戶端代碼。在com.geostar.geoglobe.jaxws目錄下產生了GeoGlobeServer.java、ObjectFactory.java、WSDemo.java三個類。

下面我看看這些類是做什麼的。

//GeoGlobeServer.java

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;

/**
 * This class was generated by the JAXWS SI.
 * JAX-WS RI 2.0_02-b08-fcs
 * Generated source version: 2.0
 *
 */
@WebServiceClient(name = "GeoGlobeServer", targetNamespace = "http://www.geostar.com.cn", wsdlLocation = "http://126.33.8.183:9999/GeoGlobeServer?wsdl")
public class GeoGlobeServer
    extends Service
{

    private final static URL GEOGLOBESERVER_WSDL_LOCATION;

    static {
        URL url = null;
        try {
            url = new URL("http://126.33.8.183:9999/GeoGlobeServer?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        GEOGLOBESERVER_WSDL_LOCATION = url;
    }

    public GeoGlobeServer(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public GeoGlobeServer() {
        super(GEOGLOBESERVER_WSDL_LOCATION, new QName("http://www.geostar.com.cn", "GeoGlobeServer"));
    }

    /**
     *
     * @return
     *     returns WSDemo
     */
    @WebEndpoint(name = "WSDemoPort")
    public WSDemo getWSDemoPort() {
        return (WSDemo)super.getPort(new QName("http://www.geostar.com.cn", "WSDemoPort"), WSDemo.class);
    }

}

//WSDemo.java

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * This class was generated by the JAXWS SI.
 * JAX-WS RI 2.0_02-b08-fcs
 * Generated source version: 2.0
 *
 */
@WebService(name = "WSDemo", targetNamespace = "http://www.geostar.com.cn")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public interface WSDemo {

    /**
     *
     * @param input
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(name = "doSomethingResponse", targetNamespace = "http://www.geostar.com.cn", partName = "doSomethingResponse")
    public String doSomething(
        @WebParam(name = "input", targetNamespace = "http://www.geostar.com.cn", partName = "input")
        String input);

}
//ObjectFactory.java

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

/**
 * This object contains factory methods for each
 * Java content interface and Java element interface
 * generated in the ClientTC package.
 * <p>An ObjectFactory allows you to programatically
 * construct new instances of the Java representation
 * for XML content. The Java representation of XML
 * content can consist of schema derived interfaces
 * and classes representing the binding of schema
 * type definitions, element declarations and model
 * groups.  Factory methods for each of these are
 * provided in this class.
 *
 */
@XmlRegistry
public class ObjectFactory {

    private final static QName _DoSomethingResponse_QNAME = new QName("http://www.geostar.com.cn", "doSomethingResponse");
    private final static QName _Input_QNAME = new QName("http://www.geostar.com.cn", "input");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ClientTC
     *
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://www.geostar.com.cn", name = "doSomethingResponse")
    public JAXBElement<String> createDoSomethingResponse(String value) {
        return new JAXBElement<String>(_DoSomethingResponse_QNAME, String.class, null, value);
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}}
     *
     */
    @XmlElementDecl(namespace = "http://www.geostar.com.cn", name = "input")
    public JAXBElement<String> createInput(String value) {
        return new JAXBElement<String>(_Input_QNAME, String.class, null, value);
    }

}

我們可以寫一個測試類別:WSDemoClient.java

代碼如下:

public class WSDemoClient {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  GeoGlobeServer  server=new GeoGlobeServer();
  WSDemo demo=server.getWSDemoPort();
  System.out.println(demo.doSomething("李克華"));

 }

}

點擊Run 運行WSDemoClient.java

在控制台我們可以看到:

hello:李克華

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.