I have SOAP Client based on HttpClient and SAAJ (provided by JDK 1.7.0_65 or used from maven dependencies - "com.sun.xml.messaging.saaj.saaj-impl.1.3.25").
When I receive the SOAP response that has UTF-8 charset with BOM, I observe the following exception:
Caused by: java.io.IOException: Unexpected characters before XML declaration
at com.sun.xml.internal.messaging.saaj.util.XMLDeclarationParser.parse(XMLDeclarationParser.java:120)[:1.7.0_65]
at com.sun.xml.internal.messaging.saaj.soap.SOAPPartImpl.lookForXmlDecl(SOAPPartImpl.java:645)[:1.7.0_65]
...
Retrived SOAP response:
[0xef][0xbb][0xbf]
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header />
env:Body
Hello, SOAP!
</env:Body>
</env:Envelope>
Sample code to reproduce a problem:
@test
public void testCreateSoapResponseWithUTF8AndBOM() throws Exception
{
try (InputStream responseContent = Resources.getTemplate("soap-request-utf8.xml-with-bom.xml"))
{ MimeHeaders mimeHeaders = new MimeHeaders(); mimeHeaders.addHeader(HttpHeaders.CONTENT_TYPE, "application/soap+xml; charset="utf-8""); MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL); SOAPMessage message = messageFactory.createMessage(mimeHeaders, responseContent); message.saveChanges(); message.getSOAPHeader(); }
}
I found a topic with the similar problem at SAAJ (dated 2008), but it doesn't describe a solution.
https://java.net/projects/saaj/lists/users/archive/2008-07/message/6
I hope for your help.
Affected Versions
[1.3.16]