javax.xml.stream.XMLStreamException 使用 Axiom 发送 SOAP 时

javax.xml.stream.XMLStreamException when using Axiom to send a SOAP

我有以下代码

    final String METHOD="test";
    final String PARAM1_VAL="Hello";
    final String TARGET_EPR="http://bhanuka-TECRA-M11:8280/services/SoapToRestProxy";

    SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
    OMNamespace samplesNamespace = factory.createOMNamespace("http://services.samples/xsd", "m");
    SOAPEnvelope envelope = factory.getDefaultEnvelope();


    OMElement requestElement = factory.createOMElement("request", samplesNamespace);
    OMElement methodElement = factory.createOMElement("method",samplesNamespace);
    OMElement param1 = factory.createOMElement("val",samplesNamespace);

    param1.setText(PARAM1_VAL);
    methodElement.setText(METHOD);
    requestElement.addChild(methodElement);
    requestElement.addChild(param1);
    envelope.getBody().addChild(requestElement);

    try {
        ServiceClient serviceClient = new ServiceClient();
        Options options = new Options();
        options.setTo(new EndpointReference(TARGET_EPR));
        serviceClient.setOptions(options);

        OMElement response = serviceClient.sendReceive(envelope);
        System.out.println(response);
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
    }

我正在做的是制作一个 SOAP 信封并将其发送到特定的端点。但是在 sendReceive 方法中,它抛出一个

javax.xml.stream.XMLStreamException: Can not output XML declaration, after other output has already been done.

我在做什么这里错了吗?有人请帮助我。

ServiceClient#sendReceive(OMElement) 不期望 SOAPEnvelope 作为输入,而是 SOAP 主体的内容。您应该传递 requestElement.

而不是传递它 envelope