javax.xml.soap.SOAPException:InputStream不代表有效的SOAP 1.1消息

Pau*_*aul 2 soap soap-client

我正在使用SOAP API。我收到的XML响应周围有一个“肥皂信封”-因此,在处理XML之前,我需要删除或解析该包装。我对其他端点采用了以下方法(至少代码是合理的),但是对于此特定端点,我会遇到错误。

我遇到的错误是:

严重:SAAJ0304:InputStream不代表有效的SOAP 1.1消息

这是我用来删除Soap Wrapper的代码:

String soapResponse = getSoapResponseFromApi();
ByteArrayInputStream inputStream = new ByteArrayInputStream(soapResponse.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, inputStream);
Document doc = message.getSOAPBody().extractContentAsDocument();   // <-- error thrown here


//unmarhsall the XML in 'doc' into an object
//do useful stuff with that object
Run Code Online (Sandbox Code Playgroud)

这是我收到的XML(上面代码中soapResponse的内容)

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <XMLContentIActuallyWant xmlns="http://my-url.com/webservices/">
            <!-- Useful stuff here -->
        </XMLContentIActuallyWant >
    </soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)

Pau*_*aul 6

我在准备这个问题时发现了解决方案。

肥皂版本具有不同的格式。SoapMessage库默认为soap 1.1-但我收到的响应内容为soap 1.2。

当我检查正在发送的完整请求以接收上述响应时,可以看到此消息-看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body> 
        <!-- xml content here --> 
  </soap12:Body>
</soap12:Envelope>
Run Code Online (Sandbox Code Playgroud)

肥皂12部分突出显示它正在请求肥皂1.2。

因此,尽管响应中不包含“ 12”-响应也位于1.2中。

因此,我们需要告诉SoapMessage使用1.2而不是默认值(在我的情况下为1.1)。

我是这样修改上面的代码来做到这一点的:

之前:

SOAPMessage message = MessageFactory.newInstance().createMessage(null, inputStream);
Run Code Online (Sandbox Code Playgroud)

后:

SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, inputStream);
Run Code Online (Sandbox Code Playgroud)

值得注意的是,同一API的其他端点都为SOAP 1.1服务-这就是为什么此错误令我感到困惑。我在做同一件事,却得到不同的结果。

  • 谢谢!为我解决了问题 (2认同)