如何从java中的wsdl文件生成soap请求xml

发布于 2024-12-02 16:49:50 字数 229 浏览 2 评论 0原文

我正在寻找一些 java 开源 api,通过将 wsdl_URL 和操作名称作为参数传递来生成soap请求xml文件。实际上,soapUI 正在执行此操作,我尝试查看soapUI 源代码,但我无法理解整个代码来完成我的任务。

有没有可用的 java api 来执行此操作(apache 或其他)?

我在网上花了几天时间,没有看到任何结果。

如果有人有任何想法请帮助我。

提前致谢。

I am looking for some java opensource api for generating soap request xml file by passing wsdl_URL and operation name as parameters. Actually soapUI is doing this and I tried to go through the soapUI source code, but I am not able to understand the whole code to get my task done.

Is there any java api available to do this (apache or something)?

I spent couple of days in the net and didn't see any result.

If any body has any idea please help me.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

注定孤独终老 2024-12-09 16:49:50

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

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);
        }
    }
}

You can use the open-source Membrane SOA library ([http://www.membrane-soa.org/soa-model-doc/1.4/java-api/create-soap-request-template.htm ]) to generate XMLs for each operation defined in the WSDL:

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);
        }
    }
}
动次打次papapa 2024-12-09 16:49:50

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 的开发者角提供了与soap集成的很好的指导用户界面 API。

Soap UI also provide Java Api for creating request and response xml from WSDL.

 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));

        }
    }

Developer's corner of Soap UI has nice pointers for integrating with soap UI Api.

野鹿林 2024-12-09 16:49:50

如果您有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);
    }

if you have SOAPHandler for request, you can print your xml like this:

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);
        }
}

and call this method in handleMessage and handleFault.

in another way if you don't use apache or another library for calling soap service, use can see MessageWrapper class constructor in jdk manually and add break point on packet variable and see p.toString() in debug mode :)

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文