【问题标题】:Java soap calls to pass an .XML file to WebServiceJava SOAP 调用将 .XML 文件传递​​给 WebService
【发布时间】:2014-05-21 15:03:59
【问题描述】:

所以我是整个 SOAP 和服务器概念的新手。我已经组合了一个基本的 JAX-RPC 和 JAX-WS。我总体上想将 .XML 文件传递​​给 Web 服务,接收响应,并将其写入目录。我从哪里开始,我应该使用什么,以及在哪里可以找到基于它的教程/信息。谢谢!

【问题讨论】:

    标签: java xml web-services soap


    【解决方案1】:

    在设计 Web 服务时,基本上可以采用两种方法。自上而下的方法和自下而上的方法。我将为您简要解释这两种方法及其优缺点。还会有一些教程的链接。

    自上而下:

    在自上而下的方法中,您首先对 XSD 进行建模,该 XSD 将包含您的请求和响应消息以及这些请求和响应将使用的数据结构。然后,您对操作进行建模,即在客户端和服务之间流动的请求和响应,最后将它们组合到 WSDL 中。然后将生成的 WSDL 导入到 Netbeans 或 Eclipse 等 IDE 中,然后您开始编写服务的内部代码。

    例如,假设您有产品服务。在此服务中,您希望创建一个将根据产品代码搜索特定产品的操作。因此,您要查询产品服务的产品对象。服务将被称为ProductService,操作将被称为GetProduct

    要实现这一点,您需要对具有两个字符串属性的产品对象进行建模,称为描述和代码。您还需要对GetProductRequest 消息和GetProductResponse 消息进行建模。

    这可能采用以下结构:

    XSD 的代码如下所示:

    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema xmlns="http://www.wsexample.com/ProductService_V1/Product"
           elementFormDefault="qualified"
           targetNamespace="http://www.wsexample.com/ProductService_V1/Product"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:simpleType name="ProductCode_Type">
        <xs:annotation>
            <xs:documentation>This the product code type. It is based on the string data type it must be 8 characters long.
    </xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:minLength value="8" />
            <xs:maxLength value="8" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="ProductDescription_Type">
        <xs:annotation>
            <xs:documentation>This is the base class for the product description field. This is a text field up to 255 characters long.</xs:documentation>
        </xs:annotation>
        <xs:restriction base="xs:string">
            <xs:maxLength value="255" />
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="Product_Type">
        <xs:annotation>
            <xs:documentation>This is the product base class it is used to perform CRUD operations with on all of the product service operations.
    </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="ProductCode"
                        type="ProductCode_Type"
                        minOccurs="1"
                        maxOccurs="1" />
            <xs:element name="ProductDescription"
                        type="ProductDescription_Type"
                        minOccurs="0"
                        maxOccurs="1" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="GetProductRequest_Type">
        <xs:annotation>
            <xs:documentation>This is the base class for the Get Product Request message. In the message you must pass one and only one product code which to search for.   </xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="Product"
                        type="Product_Type" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="GetProductResponse_Type">
        <xs:annotation>
            <xs:documentation>This is the get product response message and will contain the result of the results of calling the getproductdescription operation on the Product service.
    It will contain a product code which was passed in the Get Product Request message and optionally return one description.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="Product"
                        type="Product_Type" />
        </xs:sequence>
    </xs:complexType>
    <xs:element name="GetProductRequest"
                type="GetProductRequest_Type" />
    <xs:element name="GetProductResponse"
                type="GetProductResponse_Type" />
    </xs:schema>
    

    您现在需要创建一个新的 WSDL 来描述服务并在这个 WSDL 中使用这个 XSD(我称之为 product.xsd)。如您所见,我们已经对数据结构进行了建模以传输产品对象,并且我们已经对服务中使用的操作进行了建模。

    我们的 WSDL 可能看起来像这样:

    这是 WSDL 的代码

    <?xml version="1.0" encoding="utf-8"?>
    
     <wsdl:definitions name="ProductService"
                  targetNamespace="http://wsexample.com/ProductService"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
                  xmlns:tns="http://wsexample.com/ProductService"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:ProductData="http://www.wsexample.com/Product/ProductData">
    <wsdl:types>
        <xs:schema elementFormDefault="qualified"
                   targetNamespace="http://wsexample.com/ProductService">
            <xs:import schemaLocation="Product.xsd"
                       namespace="http://www.wsexample.com/Product/ProductData" />
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="GetProduct">
        <wsdl:part name="in"
                   element="ProductData:GetProductRequest" />
    </wsdl:message>
    <wsdl:message name="GetProductRs">
        <wsdl:part name="out"
                   element="ProductData:GetProductResponse" />
    </wsdl:message>
    <wsdl:portType name="ProductEndPoint">
        <wsdl:operation name="GetProduct">
            <wsdl:input message="tns:GetProduct" />
            <wsdl:output message="tns:GetProductRs" />
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="ProductServiceBinding"
                  type="tns:ProductEndPoint">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
                      style="document" />
        <wsdl:operation name="GetProduct">
            <wsdl:input>
             <soap:body parts="in"
                           use="literal" />
    
            </wsdl:input>
            <wsdl:output>
                <soap:body parts="out"
                           use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="ProductEndpointService">
        <wsdl:port name="ProductServiceEndPointPort"
                   binding="tns:ProductServiceBinding">
            <soap:address location="http://wsexample.com/ProductService" />
        </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>
    

    因此,在对将在客户端和服务器之间流动的消息进行建模之后,将如下所示:

    请求:

     <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   xmlns:prod="http://www.wsexample.com/Product/ProductData">
     <soapenv:Header/>
     <soapenv:Body>
       <prod:GetProductRequest>
          <prod:Product>
             <prod:ProductCode>12345678</prod:ProductCode>
    
          </prod:Product>
       </prod:GetProductRequest>
      </soapenv:Body>
    </soapenv:Envelope>
    

    回应:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://www.wsexample.com/Product/ProductData">
     <soapenv:Header/>
     <soapenv:Body>
        <prod:GetProductResponse>
           <prod:Product>
              <prod:ProductCode>12345678</prod:ProductCode>
              <!--Optional:-->
              <prod:ProductDescription>A Basic product for kids to teach them how to count.    </prod:ProductDescription>
           </prod:Product>
        </prod:GetProductResponse>
      </soapenv:Body>
    </soapenv:Envelope>
    

    现在您可以使用 WSDL 和 XSD 来实现使用此 Netbeans tutorial and guide. 的 Web 服务。它非常简单,只需在 netbeans 中启动一个新的 Web 项目,然后右键单击该项目并添加新文件,然后从 WSDL 文件中选择 Web 服务。

    请看下面的屏幕截图:

    自上而下的总结:

    自上而下要求您预先进行大量建模和规划。但是,您可以完全控制。这使您可以决定数据将如何在客户端和服务器之间流动。它允许您根据您的确切要求修改所有内容。但是,这需要大量的工作。

    对我来说最大的好处是我可以设计一个 WSDL 和 XSD,将数据从提供系统抽象为更通用的东西。这在集成项目中变得很重要。

    自下而上:

    自下而上的方法允许您从 java 代码生成我在上面创建的工件。这里有一个excellent tutorial,它将向您展示所有详细信息。然而,Java 允许您向类添加注释,然后将该类及其方法公开为 Web 服务。

    因此,通过获取一个类并添加注释,您可以将该类变成一个 Web 服务。请参阅下面的代码以获取快速而肮脏的示例:

    @Webservice
    public Class CalculatorWS
    {
    
        @WebMethod
        public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
            int k = i + j;
            return k;
        }
    }
    

    如果您按照本教程进行操作,您可能会在几分钟内从代码转换为完全正常工作的 Web 服务。快速简单。

    自下而上的总结:

    您几乎无法控制 WSDL 和 XSD 的外观和行为方式。在某些情况下,这种方法会将您紧密地绑定到底层模型。这真的取决于你如何编码。因此,使用自下而上的方法,您可以在几分钟内制作出原型,但向上和向下流动的信息可能与您的想法并不完全一致。

    重要提示:

    下载 SOAPUI 的副本,它确实是用于测试甚至创建模拟服务的最佳工具。如果您对使用网络服务很认真,那么现在就开始吧。

    希望这能帮助你走出兔子洞。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2012-01-18
      • 2017-10-19
      • 2011-04-07
      • 2017-07-03
      • 2013-02-11
      • 1970-01-01
      相关资源
      最近更新 更多