2.使用JDK开发webService

使用jdk开发webService需要注意:jdk版本必须1.6以及1.6以上!

以下webService的组成部分:

  server端和client端,通过服务器端(server)webService发布,使用客户端调用。

   说明:开发中也许只做server端也许只做client端,以下只是模拟程序。

开发步骤:

1.开发server端:

  1.1编写webService编码:

     1.1.1创建一个接口(SEI,webService终端接口,该接口方法供client端调用)

/**SEI及其实现类必须添加@WebService注解*/
@WebService
public interface HelloWS {

    /**SEI方法添加@WebMethod注解*/
    @WebMethod
    public String sayHello(String name);
}

   1.1.2定义HelloWebService接口的实现类

/**SEI实现类添加@WebService注解*/
@WebService
public class HelloWSImpl implements HelloWS {

    /**该方法用于暴露出去,目的是让客户端来调用*/
    /**
     * @param name由客户端传入
     * return String由服务器返回给客户端
     */
    @Override
    public String sayHello(String name) {
        return "hello: "+name;
    }

}

  1.2发布webService,将以上定义暴露出去以便供客户端调用

public class RealeaseWS {

    public static void main(String[] args) {
        /**address是发布的地址,只要端口不占用,任意端口*/
        /**端口后可以跟工程名等等,可以随便写*/
        String address = "http://localhost:8989/WebService_Server";
        /**
         * 使用由Endpoint来发布,这里有两个参数:
         * 参数一为发布地址,参数二为SEI接口的实现类对象
         */
        Endpoint.publish(address, new HelloWSImpl());
        /**如果发布成功,打印*/
        System.out.println("webService发布成功!");
    }
}

  执行程序:

  

2.开发client端:

新建WebService_Client工程

2.1借助jdk的wsimort.exe工具生成客户端代码

  注意:客户端代码生成到client工程下,跟server端是不一样的工程

  2.1.1使用jdk提供的wsimport生成客户端代码

    2.1.1.1开始,运行,输入cmd打开控制台窗口;

    2.1.1.2定位到客户端src目录(客户端代码要生成在这里):

      比如我的src目录为:

        D:\developUtil\workspace\WebService_Client\src,

        那么就是cd D:\developUtil\workspace\WebService_Client\src

    2.1.1.3借助wsimport生成客户端代码

      在控制台输入:wsimport -keep http://localhost:8989/WebService_Server?wsdl(该路径为服务端发布的address)

      生成后,结构图:

      

      注意:由于我没有创建包,所以包是由eclipse帮我创建的

2.2测试程序

测试之前,先看wsdl文档(注意图中粗体、红色、字体较大部分):

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.webService.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.webService.com/" name="HelloWSImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://server.webService.com/" schemaLocation="http://localhost:8989/WebService_Server?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloWSImpl">
<operation name="sayHello">
<input wsam:Action="http://server.webService.com/HelloWSImpl/sayHelloRequest" message="tns:sayHello"></input>
<output wsam:Action="http://server.webService.com/HelloWSImpl/sayHelloResponse" message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloWSImplPortBinding" type="tns:HelloWSImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWSImplService">
<port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding">
<soap:address location="http://localhost:8989/WebService_Server"></soap:address>
</port>
</service>
</definitions>

以上三个特殊的粗体部分,分别是:

<portType name="HelloWSImpl">
<service name="HelloWSImplService">
<port name="HelloWSImplPort"

说明:

HelloWSImpl就是服务端的SEI即接口;

HelloWSImplService就是服务端的SEI接口实现类;

HelloWSImplPort方法通过HelloWSImplService对象调用,返回一个HelloWSImpl代理对象,最后通过代理对象调用服务端暴露的方法。

public class TestWebService {

    public static void main(String[] args) {
        HelloWSImplService factory = new HelloWSImplService();
        HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
        String result = helloWSImpl.sayHello("webService");
        System.out.println(result);
    }
}
posted @ 2016-01-20 23:52  飘飘来来荡荡去去  阅读(270)  评论(0编辑  收藏  举报