首页 > 解决方案 > 如何设置值 SoapRequestCreator

问题描述

遇到了问题。如何将输入数据放入通过 SOARequestCreator(com.predic8.wsdl 库)生成的soap 请求中。

我的方法:

@SneakyThrows
public String parseTEst(MultipartFile file) {
    WSDLParser parser = new WSDLParser();

    Definitions defs = parser.parse(file.getInputStream());

    StringWriter writer = new StringWriter();

    HashMap<String, String> formParams = new HashMap<String, String>();
    formParams.put("xpath:/Add/intA", "1");
    formParams.put("intB", "2");

    SOARequestCreator creator = new SOARequestCreator(defs, new RequestTemplateCreator(), new MarkupBuilder(writer));
    creator.setFormParams(formParams);

    creator.createRequest("CalculatorSoap", "Add", "CalculatorSoap");

    System.out.println(writer);

    return writer.toString();
}

此方法返回:

 <s11:Envelope xmlns:s11='http://schemas.xmlsoap.org/soap/envelope/'>
  <s11:Body>
    <ns1:Add xmlns:ns1='http://tempuri.org/'>
      <ns1:intA>?999?</ns1:intA>
      <ns1:intB>?999?</ns1:intB>
    </ns1:Add>
  </s11:Body>
</s11:Envelope>

如何替换“intA”和“intB”? 我认为这很可能应该在这里完成:

HashMap<String, String> formParams = new HashMap<String, String>();
        formParams.put("xpath:/Add/intA", "1");
        formParams.put("intB", "2");

        SOARequestCreator creator = new SOARequestCreator(defs, new RequestTemplateCreator(), new MarkupBuilder(writer));
        creator.setFormParams(formParams);

因为在 tis 网站上的文档中(http://www.membrane-soa.org/soa-model-doc/1.4/java-api/create-soap-request.htm):

 HashMap<String, String> formParams = new HashMap<String, String>();
formParams.put("xpath:/create/article/name", "foo");
formParams.put("xpath:/create/article/description", "bar");
formParams.put("xpath:/create/article/price/amount", "00.00");
formParams.put("xpath:/create/article/price/currency", "USD");
formParams.put("xpath:/create/article/id", "1");
 
//SOARequestCreator constructor: SOARequestCreator(Definitions, Creator, MarkupBuilder)
SOARequestCreator creator = new SOARequestCreator(wsdl, new RequestCreator(), new MarkupBuilder(writer));
creator.setFormParams(formParams);

我的 wsdl 文件:

 <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://tempuri.org/"
                  xmlns:s="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/"
                  targetNamespace="http://tempuri.org/">
    <wsdl:types>
        <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
            <s:element name="Add">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="intA" type="s:int"/>
                        <s:element minOccurs="1" maxOccurs="1" name="intB" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="AddResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="AddResult" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="Subtract">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="intA" type="s:int"/>
                        <s:element minOccurs="1" maxOccurs="1" name="intB" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="SubtractResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="SubtractResult" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="Multiply">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="intA" type="s:int"/>
                        <s:element minOccurs="1" maxOccurs="1" name="intB" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="MultiplyResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="MultiplyResult" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="Divide">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="intA" type="s:int"/>
                        <s:element minOccurs="1" maxOccurs="1" name="intB" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
            <s:element name="DivideResponse">
                <s:complexType>
                    <s:sequence>
                        <s:element minOccurs="1" maxOccurs="1" name="DivideResult" type="s:int"/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:schema>
    </wsdl:types>
    <wsdl:message name="AddSoapIn">
        <wsdl:part name="parameters" element="tns:Add"/>
    </wsdl:message>
    <wsdl:message name="AddSoapOut">
        <wsdl:part name="parameters" element="tns:AddResponse"/>
    </wsdl:message>
    <wsdl:message name="SubtractSoapIn">
        <wsdl:part name="parameters" element="tns:Subtract"/>
    </wsdl:message>
    <wsdl:message name="SubtractSoapOut">
        <wsdl:part name="parameters" element="tns:SubtractResponse"/>
    </wsdl:message>
    <wsdl:message name="MultiplySoapIn">
        <wsdl:part name="parameters" element="tns:Multiply"/>
    </wsdl:message>
    <wsdl:message name="MultiplySoapOut">
        <wsdl:part name="parameters" element="tns:MultiplyResponse"/>
    </wsdl:message>
    <wsdl:message name="DivideSoapIn">
        <wsdl:part name="parameters" element="tns:Divide"/>
    </wsdl:message>
    <wsdl:message name="DivideSoapOut">
        <wsdl:part name="parameters" element="tns:DivideResponse"/>
    </wsdl:message>
    <wsdl:portType name="CalculatorSoap">
        <wsdl:operation name="Add">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Adds two integers. This is a test
                WebService. ©DNE Online
            </wsdl:documentation>
            <wsdl:input message="tns:AddSoapIn"/>
            <wsdl:output message="tns:AddSoapOut"/>
        </wsdl:operation>
        <wsdl:operation name="Subtract">
            <wsdl:input message="tns:SubtractSoapIn"/>
            <wsdl:output message="tns:SubtractSoapOut"/>
        </wsdl:operation>
        <wsdl:operation name="Multiply">
            <wsdl:input message="tns:MultiplySoapIn"/>
            <wsdl:output message="tns:MultiplySoapOut"/>
        </wsdl:operation>
        <wsdl:operation name="Divide">
            <wsdl:input message="tns:DivideSoapIn"/>
            <wsdl:output message="tns:DivideSoapOut"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="CalculatorSoap" type="tns:CalculatorSoap">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="Add">
            <soap:operation soapAction="http://tempuri.org/Add" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Subtract">
            <soap:operation soapAction="http://tempuri.org/Subtract" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Multiply">
            <soap:operation soapAction="http://tempuri.org/Multiply" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Divide">
            <soap:operation soapAction="http://tempuri.org/Divide" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:binding name="CalculatorSoap12" type="tns:CalculatorSoap">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="Add">
            <soap12:operation soapAction="http://tempuri.org/Add" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Subtract">
            <soap12:operation soapAction="http://tempuri.org/Subtract" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Multiply">
            <soap12:operation soapAction="http://tempuri.org/Multiply" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="Divide">
            <soap12:operation soapAction="http://tempuri.org/Divide" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap12:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="Calculator">
        <wsdl:port name="CalculatorSoap" binding="tns:CalculatorSoap">
            <soap:address location="http://www.dneonline.com/calculator.asmx"/>
        </wsdl:port>
        <wsdl:port name="CalculatorSoap12" binding="tns:CalculatorSoap12">
            <soap12:address location="http://www.dneonline.com/calculator.asmx"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

请帮帮我! 谢谢!

标签: javasoap

解决方案


很可能是因为 xpath 表达式与第一个不同?第二个没有 xpath: 前缀?那仍然不能解释为什么第一个不起作用。

formParams.put("xpath:/Add/intA", "1");
formParams.put("intB", "2");

推荐阅读