java6 WebServices客户端

阅读更多

在J ava6 WebServices 服务端 这篇文章中和大家分享了Java6 WebServices 服务端的写法,光有服务端还不行,还要有客户端才行啊。看了一些网友的相关文章,都只给出了服务端的写法,没有说客户端怎么说。经过一番研究,终于搞定了客户端,今天我就和大家分享下。
首先启动 Java6 WebServices 服务端 一文中写好的服务端。
新建个项目webserviceclient。命令行到src目录执行,wsimport -keep http://localhost:5050/HelloService?wsdl
会自动生成以下代码:

package cn.cissco;

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 JAX-WS RI.
* JAX-WS RI 2.1.1 in JDK 6
* Generated source version: 2.1
* 
*/
@WebService(name = "Hello", targetNamespace = "http://www.cissco.cn")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface Hello {


    /**
     * 
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    public String sayHello(
        @WebParam(name = "arg0", partName = "arg0")
        String arg0);

}



package cn.cissco;

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;
import javax.xml.ws.WebServiceFeature;


/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.1 in JDK 6
* Generated source version: 2.1
* 
*/
@WebServiceClient(name = "HelloService", targetNamespace = "http://www.cissco.cn", wsdlLocation = "http://localhost:5050/HelloService?wsdl")
public class HelloService
    extends Service
{

    private final static URL HELLOSERVICE_WSDL_LOCATION;

    static {
        URL url = null;
        try {
            url = new URL("http://localhost:5050/HelloService?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HELLOSERVICE_WSDL_LOCATION = url;
    }

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

    public HelloService() {
        super(HELLOSERVICE_WSDL_LOCATION, new QName("http://www.cissco.cn", "HelloService"));
    }

    /**
     * 
     * @return
     *     returns Hello
     */
    @WebEndpoint(name = "HelloPort")
    public Hello getHelloPort() {
        return (Hello)super.getPort(new QName("http://www.cissco.cn", "HelloPort"), Hello.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features parameter will have their default values.
     * @return
     *     returns Hello
     */
    @WebEndpoint(name = "HelloPort")
    public Hello getHelloPort(WebServiceFeature... features) {
        return (Hello)super.getPort(new QName("http://www.cissco.cn", "HelloPort"), Hello.class, features);
    }

}

 

然后再写一个测试类ClientTest.java

package cn.cissco;

public class ClientTest {

/**
* @param args
*/
public static void main(String[] args) {
   // TODO Auto-generated method stub
   HelloService service = new HelloService();
   Hello h = service.getHelloPort();
   System.out.println(h.sayHello("Cissco"));
}
}

 

执行,控制台能输出:"Cissco Say Hello!"

OK 成功,java6编写WebServices就这么简单

你可能感兴趣的:(WebService,XML,JDK,SOAP,.net)