How to deal with invalid characters in a WS output when using CXF?

前端 未结 4 1513
感动是毒
感动是毒 2021-02-07 06:00

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.

The problem is that some entri

相关标签:
4条回答
  • 2021-02-07 06:23
    /**
    * 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);
    }
    

    source: http://www.theplancollection.com/house-plan-related-articles/hexadecimal-value-invalid-characterheplancollection.com/house-plan-related-articles/hexadecimal-value-invalid-character

    0 讨论(0)
  • 2021-02-07 06:25

    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:

    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);
    }
    
    0 讨论(0)
  • 2021-02-07 06:26

    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

    Seems like there is a discussion on the issue here: 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.

    Hope this helps a bit.

    0 讨论(0)
  • 2021-02-07 06:34

    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).

    You can find more details here.

    HTH!

    0 讨论(0)
提交回复
热议问题