javaweb-servicessoapapache-axisaxiom

Hot to get SOAPBody String in axis2


guys! I am trying to get String of SOAPBody object request. I am using axis2 and Tomcat 9 for web-services.

public static String getXMLSoapBody()
{
   log.getLogger().debug("TEST - getXmlSoapBody - START");
   String soapBody;
   SOAPBody body;
   MessageContext messageContext = MessageContext.getCurrentMessageContext();
   try {
      SOAPEnvelope env  = messageContext.getEnvelope();
      body = env.getBody();
   }
   catch(Exception e)
   {
      log.getLogger().error("SOAP Exception :" + e.toString());
      e.printStackTrace();

      return null;
   }

   soapBody = body.toString();

return soapBody;

But when I am sending request I get this error: catalina.out: 10:41:26.935 [http-nio-8080-exec-2] ERROR org.apache.axis2.transport.http.AxisServlet - processAxisFault() found a null HTTP status from the MessageContext instance, setting HttpServletResponse status to: axis2.http.response.state

my log file: 2022-12-01 10:41:26 [http-nio-8080-exec-2] (TransUtils.java:287) DEBUG TEST - getXmlSoapBody - START

I tried to see full stacktrace of this, but when I trying to use try/catch for soapBody = body.toString(); line I am getting this error but the previous one is not:

org.apache.axiom.om.NodeUnavailableException
    at org.apache.axiom.om.impl.common.AxiomExceptionTranslator.translate(AxiomExceptionTranslator.java:35)
    at org.apache.axiom.om.impl.llom.AxiomContainerImpl.serialize(Unknown Source)
    at org.apache.axiom.om.impl.llom.AxiomContainerImpl.serializeAndSurfaceIOException(Unknown Source)
    at org.apache.axiom.om.impl.llom.AxiomContainerImpl.serialize(Unknown Source)
    at org.apache.axiom.om.impl.llom.AxiomContainerImpl.serialize(Unknown Source)
    at org.apache.axiom.om.impl.llom.AxiomContainerImpl.serialize(Unknown Source)
    at org.apache.axiom.om.impl.llom.AxiomElementImpl.toString(Unknown Source)
    at uk.co.celesio.orders.server.TransUtils.getXMLSoapBody(TransUtils.java:304)

My request:

<?xml version='1.0' encoding='windows-1252'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soapenv:Body>
    <MyTestRequest xmlns="...">
      ...
    </MyTestRequest>
  </soapenv:Body>
</soapenv:Envelope>

Tried to use try/catch to see full error, also tried to log this, but in both cases I am getting NodeUnavailableException


Solution

  • NodeUnavailableException indicates that the body of the SOAP message has already been consumed, most likely by a data binding that converted it to a Java object (Unfortunately you only showed part of the stack trace, so it's not possible to tell that from the information provided).

    Regarding the right solution for this problem: that depends on what you are actually trying to achieve (This might be an XY problem).