Java 使用 CXF 时如何处理 WS 输出中的无效字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9710185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deal with invalid characters in a WS output when using CXF?
提问by Elias Dorneles
I'm using Spring, CXF and Hibernate to build a WebService that perform search queries on a foreign database that I have read-only access.
我正在使用 Spring、CXF 和 Hibernate 构建一个 WebService,该服务在我具有只读访问权限的外部数据库上执行搜索查询。
The problem is that some entries in the database have strange characters (0x2) in text fields, and it seems that CXF or the library (Aegis?) that it uses to process/serialize the objects returned from the Hibernate session can't deal with it:
问题是数据库中的某些条目在文本字段中具有奇怪的字符 (0x2),并且它用于处理/序列化从 Hibernate 会话返回的对象的 CXF 或库(Aegis?)似乎无法处理它:
org.apache.cxf.aegis.DatabindingException: Error writing document.. Nested exception is com.ctc.wstx.exc.WstxIOException: Invalid white space character (0x2) in text to output (in xml 1.1, could output as a character entity)
How do I get around that? Ideally, I could just remove those characters, since they don't matter for my output... Thanks!
我该如何解决?理想情况下,我可以删除这些字符,因为它们对我的输出无关紧要...谢谢!
采纳答案by nDijax
/**
* From xml spec valid chars:<br>
* #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]<br>
* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.<br>
* @param text The String to clean
* @param replacement The string to be substituted for each match
* @return The resulting String
*/
public static String CleanInvalidXmlChars(String text, String replacement) {
String re = "[^\u0009\r\n\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF]";
return text.replaceAll(re, replacement);
}
来源:http: //www.theplancollection.com/house-plan-related-articles/hexadecimal-value-invalid-characterheplancollection.com/house-plan-related-articles/hexadecimal-value-invalid-character
回答by Jarle Hansen
I am not sure this answers your question, but here is what I found.
我不确定这能回答你的问题,但这是我发现的。
Here is the class that throws the exception: http://svn.codehaus.org/woodstox/wstx/trunk/src/java/com/ctc/wstx/api/InvalidCharHandler.java
这是引发异常的类:http: //svn.codehaus.org/woodstox/wstx/trunk/src/java/com/ctc/wstx/api/InvalidCharHandler.java
Seems like there is a discussion on the issue here: http://comments.gmane.org/gmane.comp.apache.cxf.user/4373
似乎这里有一个关于这个问题的讨论:http: //comments.gmane.org/gmane.comp.apache.cxf.user/4373
Maybe this might can you:You can also set a "disable.outputstream.optimization" property on the endpoint/bus to true to have it disable the direct writing to the outputstream and always go through the XMLStreamWriter. Should accomplish the same thing without the overhead of having the SAAJModel created.
也许这可能对您有用:您还可以将端点/总线上的“disable.outputstream.optimization”属性设置为 true 以使其禁用直接写入输出流并始终通过 XMLStreamWriter。应该在没有创建 SAAJModel 的开销的情况下完成相同的事情。
Hope this helps a bit.
希望这个对你有帮助。
回答by Lizard
The top-rated answer didn't work for me, as the given Unicode encoding was rejected. With a slight alteration however, it displayed the desired behaviour:
评分最高的答案对我不起作用,因为给定的 Unicode 编码被拒绝。然而,稍加改动,它显示了所需的行为:
public static String CleanInvalidXmlChars(String text, String replacement) {
String re = "[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u0001\u0000-\u0010\uFFFF]";
return text.replaceAll(re, replacement);
}
回答by cristianoms
To achieve the desired behaviour and avoid exceptions being thrown, you'll have to extend the default Woodstoks factory com.ctc.wstx.stax.WstxOutputFactory
with your own's, that's supposed only to overwrite the property com.ctc.wstx.outputInvalidCharHandler
with an instance of com.ctc.wstx.api.InvalidCharHandler.ReplacingHandler
. This handler takes as constructor argument the replacement char to the invalid ones. With your instance in hand, create a file named META-INF/services/javax.xml.stream.XMLOutputFactory
and place inside it only the complete name of your implementation (make sure it'll be placed inside the META-INF/services directory in the resulting jar).
为了实现所需的行为并避免引发异常,您必须com.ctc.wstx.stax.WstxOutputFactory
使用自己的工厂扩展默认的 Woodstok 工厂,这应该只是com.ctc.wstx.outputInvalidCharHandler
用com.ctc.wstx.api.InvalidCharHandler.ReplacingHandler
. 此处理程序将无效字符的替换字符作为构造函数参数。使用您的实例,创建一个名为的文件,META-INF/services/javax.xml.stream.XMLOutputFactory
并在其中仅放置实现的完整名称(确保将其放置在生成的 jar 中的 META-INF/services 目录中)。
You can find more details here.
您可以在此处找到更多详细信息。
HTH!
哼!