WebService是SOA的基石, Oracle, Microsoft, IBM 等多家公司参与其中, 确保其互联互通. JDK 1.6 内置了 JAX-WS 2.1(https://jax-ws.dev.java.net/), 可直接开发部署Web Service, 也可方便的根据WSDL生成 Web Service 客户端.

 

服务端

/*

 * Copyright 2007 Sun Microsystems, Inc.

 * All rights reserved.  You may not modify, use,

 * reproduce, or distribute this software except in

 * compliance with  the terms of the License at:

 * http://developer.sun.com/berkeley_license.html

 */

 

 

package helloservice.endpoint;

 

 

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.xml.ws.Endpoint;

 

 

@WebService

public class Hello {

    private String message = new String("Hello, ");

 

    @WebMethod

    public String sayHello(String name) {

        return message + name + ".";

    }

    

    public static void main(String[] args) {

        Hello hello = new Hello();   

           Endpoint endpoint = Endpoint.publish("http://localhost:8080/helloservice/hello?wsdl", hello);

    }

}

</p>

编写完成后直接运行即可, Web 服务即监听在给定端口和地址.

 

客户端

src/simpleclient/HelloClient.java

/*

 * Copyright 2007 Sun Microsystems, Inc.

 * All rights reserved.  You may not modify, use,

 * reproduce, or distribute this software except in

 * compliance with  the terms of the License at:

 * http://developer.sun.com/berkeley_license.html

 */

 

 

package simpleclient;

 

import javax.xml.ws.WebServiceRef;

import helloservice.endpoint.HelloService;

import helloservice.endpoint.Hello;

 

 

public class HelloClient {

    @WebServiceRef(wsdlLocation = "http://localhost:8080/helloservice/hello?wsdl")

    static HelloService service;

 

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        try {

            service = new HelloService();

            HelloClient client = new HelloClient();

            client.doTest(args);

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

 

    public void doTest(String[] args) {

        try { // Call Web Service Operation

            System.out.println(

                    "Retrieving the port from the following service: "

                    + service);

 

            Hello port = service.getHelloPort();

 

            System.out.println("Invoking the sayHello operation on the port.");

 

            String name;

 

            if (args.length > 0) {

                name = args[0];

            } else {

                name = "No Name, This is BeanSoft.biz 呵呵";

            }

 

            String response = port.sayHello(name);

            System.out.println(response);

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }

}

</p>

此代码目前还无法编译, 需要首先运行下列命令:

wsimport -d src -Xnocompile http://localhost:8080/helloservice/hello?wsdl

此时将会在src/helloservice/endpoint目录下生成一系列文件:

**_Hello.java              HelloService.java       ObjectFactory.java</p>

package-info.java       SayHello.java           SayHelloResponse.java</em></strong>

编译后, 运行 HelloClient, 即可完成对Web服务的调用.

转载请注明:WebLogic Android 博客 » 使用JDK 1.6 开发简单的WebService[入门]