javasoapapache-axissoap-clientaxiom

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


I have the following code

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

What I am doing is making a SOAP envelope and sending it to a specific Endpoint. but in the sendReceive method, it throws an

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

What I am doing Wrong here ? somebody please help me.


Solution

  • ServiceClient#sendReceive(OMElement) doesn't expect a SOAPEnvelope as input, but the content of the SOAP body. Instead of passing it envelope, you should pass it requestElement.