学习 WebService 第三步:一个简单的实例(RAD+WAS 8.5开发SOAP项目)

【开发环境】

  • Web Service 服务器端开发工具:RAD(Eclipse内核)
  • Web Service 服务器:IBM WebSphere v8.5
  • REST/SOAP:SOAP(JAX-WS/SAAJ:JAX-WS)

功能非常初级,客户端→服务器端 input 一个字符串xxx,服务器端返回“xxx 您好!现在时间是:2018年3月17日23:57:41”

参考资料http://blog.csdn.net/yangwenxue_admin/article/details/51059125 (部分参考)

  • 开发服务器端(本文)
  • 开发客户端(请参考↑的参考资料,本文略)

第一步、开发服务器端

每个Web Service组件需要2个部分:接口实现类← 似乎只有类就行,这个例子是我第一个WebService 项目,先按参考资料模仿着做,以后再逐渐追加更多的内容。

1、新建Dynamic Web Project工程 TestWebService。(如果在Wizards看不到,勾选Show all wizards)

我的RAD环境和Websphere是配置好的,也可以用其他开发环境和服务器,请确保服务器能正确启动。

2、在工程中创建包 test.ws.soap (创建包不是必要的,不过是好习惯)

3、开发一个 WebService 业务接口,方法使用@WebService修饰。

在包中创建interface如下,WebInterface。

 1 package test.ws.soap;
 2 
 3 import javax.jws.WebService;
 4 
 5 @WebService
 6 public interface WebInterface {
 7     
 8     String WebMethod(String str);
 9 
10 }

4、写一个这个接口的实现类 WebClass,也需要使用@WebService修饰,并指定所需要实现的接口、及服务名称。

 1 package test.ws.soap;
 2 
 3 import javax.jws.WebService;
 4 import java.util.Date;
 5 
 6 @WebService(endpointInterface="test.ws.soap.WebInterface",serviceName="TestWS")
 7 public class WebClass implements WebInterface {
 8 
 9     @Override
10     public String WebMethod(String str) {
11 
12         return str+" Hello! now is "+new Date();
13     }
14 }

5、在浏览器中输入:http://127.0.0.1:9080/TestWebService/TestWS?wsdl 查看结果,如果成功生成如下 WSDL 文档则表示 Web Service 暴露成功。

http://127.0.0.1:9080/工程名/服务名?wsdl

http://127.0.0.1:9080/TestWebService/TestWS?wsdl

WSDL 文档

WSDL 划重点 类=端口 port,方法=操作 operation,参数=消息 message

WSDL 更详细的说明,参考:学习 WebService 第二步:知识准备——WSDL文件解析

<definitions name="TestWS" targetNamespace="http://soap.ws.test/">

    <types>

        <xsd:schema>

            <xsd:import namespace="http://soap.ws.test/"

                schemaLocation="TestWS_schema1.xsd" />

        </xsd:schema>

    </types>

    <message name="WebMethod">

        <part name="parameters" element="tns:WebMethod">

        </part>

    </message>

    <message name="WebMethodResponse">

        <part name="parameters" element="tns:WebMethodResponse">

        </part>

    </message>

    <portType name="WebInterface">

        <operation name="WebMethod">

            <input message="tns:WebMethod"

                wsam:Action="http://soap.ws.test/WebInterface/WebMethodRequest">

            </input>

            <output message="tns:WebMethodResponse"

                wsam:Action="http://soap.ws.test/WebInterface/WebMethodResponse">

            </output>

        </operation>

    </portType>

    <binding name="WebClassPortBinding" type="tns:WebInterface">

        <soap:binding style="document"

            transport="http://schemas.xmlsoap.org/soap/http" />

        <operation name="WebMethod">

            <soap:operation soapAction="" />

            <input>

                <soap:body use="literal" />

            </input>

            <output>

                <soap:body use="literal" />

            </output>

        </operation>

    </binding>

    <service name="TestWS">

        <port name="WebClassPort" binding="tns:WebClassPortBinding">

            <soap:address location="http://127.0.0.1:9080/TestWebService/TestWS" />

        </port>

    </service>

</definitions>

第一步、开发客户端

先不做了,因为主要想用SoapUI测试WebService API,所以做个Server端即可,Client端,大家有兴趣请参考:http://blog.csdn.net/jackphang/article/details/8788178

原文地址:https://www.cnblogs.com/dlsunf/p/8592150.html