Web Services 如何在java中从wsdl文件生成soap请求xml

zi8p0yeb  于 2022-11-15  发布在  Java
关注(0)|答案(4)|浏览(586)

我正在寻找一些java开源api通过传递wsdl_URL和操作名作为参数来生成soap请求xml文件。实际上soapUI正在做这件事,我试图通过soapUI源代码,但我不能理解整个代码来完成我的任务。
有没有什么java api可以做到这一点(apache或其他什么)?
我在网上花了几天时间,没有看到任何结果。
如果有人有任何想法请帮助我。
先谢谢你。

6jjcrrmo

6jjcrrmo1#

您可以使用开源的Membrane SOA库([ http://www.membrane-soa.org/soa-model-doc/1.4/java-api/create-soap-request-template.htm ])为WSDL中定义的每个操作生成XML:

public void createTemplates(String url){

    WSDLParser parser = new WSDLParser();
    Definitions wsdl = parser.parse(url);   
    StringWriter writer = new StringWriter();
    SOARequestCreator creator = new SOARequestCreator();
    creator.setBuilder(new MarkupBuilder(writer));
    creator.setDefinitions(wsdl);

    for (Service service : wsdl.getServices()) {
        for (Port port : service.getPorts()) {
            Binding binding = port.getBinding();
            PortType portType = binding.getPortType();
            for (Operation op : portType.getOperations()) {
                creator.setCreator(new RequestTemplateCreator());
                creator.createRequest(port.getName(), op.getName(), binding.getName());
                System.out.println(writer);
                writer.getBuffer().setLength(0);
        }
    }
}
qxsslcnc

qxsslcnc2#

Soap UI还提供了Java Api,用于从WSDL创建请求和响应xml。

public static void main(String[] args) throws Exception {
        WsdlProject project = new WsdlProject();
        WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "http://localhost:8080/Service?wsdl");
        WsdlInterface wsdl = wsdls[0];
        for (Operation operation : wsdl.getOperationList()) {
            WsdlOperation wsdlOperation = (WsdlOperation) operation;
            System.out.println("Request:\n"+wsdlOperation.createRequest(true));
            System.out.println("\nResponse:\n"+wsdlOperation.createResponse(true));

        }
    }

Soap UI的Developer's corner有很好的指针用于与soap UI Api集成。

ryevplcw

ryevplcw4#

如果您有SOAPHandler请求,您可以如下打印xml:

public static String getRawXml(SOAPMessageContext context) {
        try {
            ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
            context.getMessage().writeTo(byteOS);
            return byteOS.toString("UTF-8");
        } catch (SOAPException | IOException e) {
            throw new RuntimeException(e);
        }
}

并在handleMessagehandleFault中调用此方法。
另一方面,如果您不使用apache或其他库来调用soap服务,则可以手动查看jdk中MessageWrapper类构造函数,并在packet变量上添加断点,然后在调试模式中查看p.toString():)

MessageWrapper(Packet p, Message m) {
        super(m.getSOAPVersion());
        this.packet = p;
        this.delegate = m;
        this.streamDelegate = m instanceof StreamMessage ? (StreamMessage)m : null;
        this.setMessageMedadata(p);
    }

相关问题