基于JDK1.6的Web Service

Web Service 是一种网络服务,具有跨平台的优点,一种开发语言生成的Web Service接口,可以供其他语言调用,而RMI只能由本语言调用。

在JDK1.6的环境下,生成Web Service的步骤如下:

1.编写服务类

package com.webservice.server.resource;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;

@WebService(targetNamespace="")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class WebServiceServer {

 public void init(){
  String url = "http://localhost:8089/myWebService";
  System.out.println("发布Web Service:"+url);
  Endpoint.publish(url, new WebServiceServer());
 }
 
 public String doSomething(String param){
  return "web service:"+param;
 }
 
 public static void main(String[] args) {
  new WebServiceServer().init();
 }

}

 

2.启动Web Service服务

运行类WebServiceServer.java

访问URL:http://localhost:8089/myWebService?wsdl

得到以下的页面,说明启动一个Web Service成功

基于JDK1.6的Web Service_第1张图片

3.生成接口

在命令行键入:wsimport -s d:/test -keep http://localhost:8089/myWebService?wsdl

此时会在文件test下生成以下文件


package com.webservice.server;

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 = "WebServiceServer", targetNamespace = "http://server.webservice.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface WebServiceServer {


    /**
     *
     */
    @WebMethod
    public void init();

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

}

 

 


package com.webservice.server;

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 = "WebServiceServerService", targetNamespace = "http://server.webservice.com/", wsdlLocation = "http://localhost:8089/myWebService?wsdl")
public class WebServiceServerService
    extends Service
{

    private final static URL WEBSERVICESERVERSERVICE_WSDL_LOCATION;

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

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

    public WebServiceServerService() {
        super(WEBSERVICESERVERSERVICE_WSDL_LOCATION, new QName("http://server.webservice.com/", "WebServiceServerService"));
    }

    /**
     *
     * @return
     *     returns WebServiceServer
     */
    @WebEndpoint(name = "WebServiceServerPort")
    public WebServiceServer getWebServiceServerPort() {
        return (WebServiceServer)super.getPort(new QName("http://server.webservice.com/", "WebServiceServerPort"), WebServiceServer.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns WebServiceServer
     */
    @WebEndpoint(name = "WebServiceServerPort")
    public WebServiceServer getWebServiceServerPort(WebServiceFeature... features) {
        return (WebServiceServer)super.getPort(new QName("http://server.webservice.com/", "WebServiceServerPort"), WebServiceServer.class, features);
    }

}

 

4.客户端访问Web Service

package com.webservice.client;

import com.webservice.server.WebServiceServerService;

public class WebServiceClient {

 public static void main(String[] args) {
  WebServiceServerService service = new WebServiceServerService();
  String returnValue = service.getWebServiceServerPort().doSomething("hello world");
  System.out.println("returnValue="+returnValue);
 }

}

你可能感兴趣的:(基于JDK1.6的Web Service)