SOAP主体包含UTF-8 BOM时的JAX-WS错误

我使用部署在WebLogic 10.3上的JAX-WS(v2.1.3 – Sun JDK 1.6.0_05)开发了一个Web服务,当我使用Java客户端或SoapUI或其他Web服务测试工具时,它可以正常工作。 我需要使用2005 Microsoft SQL Server Reporting Services来使用此服务,我收到以下错误

由于exception而无法创建SOAP消息:XML reader错误:意外的字符内容

SEVERE: Couldn't create SOAP message due to exception: XML reader error: unexpected character content: "?" com.sun.xml.ws.protocol.soap.MessageCreationException: Couldn't create SOAP message due to exception: XML reader error: unexpected character content: "?" at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:292) at com.sun.xml.ws.transport.http.HttpAdapter.decodePacket(HttpAdapter.java:276) at com.sun.xml.ws.transport.http.HttpAdapter.access$500(HttpAdapter.java:93) at com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:432) at com.sun.xml.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244) at com.sun.xml.ws.transport.http.servlet.ServletAdapter.handle(ServletAdapter.java:134) at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doGet(WSServletDelegate.java:129) at com.sun.xml.ws.transport.http.servlet.WSServletDelegate.doPost(WSServletDelegate.java:160) at com.sun.xml.ws.transport.http.servlet.WSServlet.doPost(WSServlet.java:75) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227) at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3498) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(Unknown Source) at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180) at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086) at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406) at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201) at weblogic.work.ExecuteThread.run(ExecuteThread.java:173) Caused by: com.sun.xml.ws.streaming.XMLStreamReaderException: XML reader error: unexpected character content: "?" at com.sun.xml.ws.streaming.XMLStreamReaderUtil.nextElementContent(XMLStreamReaderUtil.java:102) at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:174) at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:296) at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:128) at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java:287) ... 22 more 

如果我使用HTTP代理来嗅出SSRS发送给JAX-WS的内容,我会看到EF BB BF作为post主体的开头而JAX-WS不喜欢这样。 如果我删除特殊字符并使用Fiddler重新提交请求,那么Web服务调用将起作用。

为什么JAX-WS会破坏标准的UTF-8 BOM? 是否有解决方法来解决此问题? 任何建议将不胜感激。 谢谢

–Vinny

我们在将.Net客户端编写到第三方Java Web服务时遇到了类似的问题,.Net包含字节顺序标记,Java服务会抛出exception。

第三方SOAP方法将一个字符串作为参数,该字符串是一个XML文档(我喜欢那些不太了解SOAP试图解决的问题的人!)默认情况下.net添加了UTF-8字节顺序标记为“有效负载”xml文档,这是严格正确的,但在实践中会导致问题。

在我们的例子中,我们从客户端(.net)端找到了两种可能的解决方案。 我不确定从SQL Reporting服务做起来会有多容易。

String.Trim() – 在传入soap方法之前,必须将xml放入字符串中,调用.Trim()删除字节顺序标记。 简单。

第二种方法是在XmlWriterSettings上设置UTF编码设置略有不同,如下所示:

 XmlWriter xmlWriter = null; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.Encoding = new UTF8Encoding(false); xmlWriter = XmlWriter.Create(xmlSteam, settings); 

重要的一点是“new UTF8Encoding( false );”,该参数是“encoderShouldEmitUTF8Identifier”并且几乎​​解决了这个问题。

一种解决方法的想法:在将请求文档传递给JAX-WS服务之前,在仅消耗BOM的Web应用程序上添加filter。