简体   繁体   English

如何在Java中使用SoapUI生成示例SOAP请求

[英]How to generate example SOAP request with SoapUI in java

I would like to write some code in Java, which will be generate examples SOAP request for given WSDL files. 我想用Java编写一些代码,这将为给定的WSDL文件生成示例SOAP请求。 The code below do it for every opperation of web services, but instead of example data in SOAP request. 下面的代码针对Web服务的每种操作执行此操作,但是代替了SOAP请求中的示例数据。 I have question mark. 我有问号。 I don't need execute request, but only write this request. 我不需要执行请求,而只需编写此请求。 I know that it is possible(like in SoapUi) but I don't know how I can do it. 我知道这是可能的(就像在SoapUi中一样),但我不知道该怎么做。

I'll be grateful for Yours help. 谢谢您的帮助。

Best regards. 最好的祝福。 Lukasz 卢卡斯

import com.eviware.soapui.impl.wsdl.*;
public class Main {
    public static void main(String[] args) {
        WsdlProject project = new WsdlProject();
        WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "sample.wsdl");
        WsdlInterface wsdl = wsdls[0];

        for (Operation operation : wsdl.getOperationList()) {
            WsdlOperation op = (WsdlOperation) operation;
            WsdlRequest request = op.addNewRequest("My Request");
            System.out.println("#" + op.getName() + "#\n" + op.createRequest(true));
        }
   }    
}

try this method: 试试这个方法:

public String getRequestResult(String wsdl, String operationName)
        throws Exception {
    // create new project
    WsdlProjectPro project = new WsdlProjectPro();

    // import amazon wsdl
    WsdlInterface iface = WsdlInterfaceFactory.importWsdl(project, wsdl,
            true)[0];

    // get desired operation
    WsdlOperation operation = (WsdlOperation) iface
            .getOperationByName(operationName);

    // create a new empty request for that operation
    WsdlRequest request = operation.addNewRequest("Java Req");

    // generate the request content from the schema
    request.setRequestContent(operation.createRequest(true));

    // submit the request
    @SuppressWarnings("rawtypes")
    WsdlSubmit submit = (WsdlSubmit) request.submit(new WsdlSubmitContext(
            request), false);

    // wait for the response
    Response response = submit.getResponse();

    return response.getContentAsString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM