什么是JacksonJsonProvider中的GSON disableHtmlEscaping等价物

时间:2015-05-19 02:40:01

标签: java rest jackson gson hateoas

我正在尝试转换网址,例如 https://api.test.com/cusomter?customer_id=1&customer_type=A

但在序列化过程中它被转换为 https://api.test.com/customer?customer_id \ u003d1 \ u0026customer_type \ u003dA

我知道在GSON中有disableHtmlEscaping选项来逃避=和&的html安全转换字符。

您能否知道JacksonJsonProvider中的等值选项。

1 个答案:

答案 0 :(得分:1)

我在here

上找到了此示例代码
import org.codehaus.jackson.SerializableString;
import org.codehaus.jackson.io.CharacterEscapes;

// First, definition of what to escape
public class HTMLCharacterEscapes extends CharacterEscapes
{
    private final int[] asciiEscapes;

    public HTMLCharacterEscapes()
    {
        // start with set of characters known to require escaping (double-quote, backslash etc)
        int[] esc = CharacterEscapes.standardAsciiEscapesForJSON();
        // and force escaping of a few others:
        esc['<'] = CharacterEscapes.ESCAPE_STANDARD;
        esc['>'] = CharacterEscapes.ESCAPE_STANDARD;
        esc['&'] = CharacterEscapes.ESCAPE_STANDARD;
        esc['\''] = CharacterEscapes.ESCAPE_STANDARD;
        asciiEscapes = esc;
    }
    // this method gets called for character codes 0 - 127
    @Override public int[] getEscapeCodesForAscii() {
        return asciiEscapes;
    }
    // and this for others; we don't need anything special here
    @Override public SerializableString getEscapeSequence(int ch) {
        // no further escaping (beyond ASCII chars) needed:
        return null;
    }
}

// and then an example of how to apply it
public ObjectMapper getEscapingMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.getJsonFactory().setCharacterEscapes(new HTMLCharacterEscapes());
    return mapper;
}

// so we could do:
public byte[] serializeWithEscapes(Object ob) throws IOException
{
    return getEscapingMapper().writeValueAsBytes(ob);
}