输出json字符串转义
import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
import org.apache.commons.lang3.text.translate.LookupTranslator;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.StringHttpMessageConverter;
import java.io.IOException;
import java.nio.charset.Charset;
public class MyStringHttpMessageConverter extends StringHttpMessageConverter {
public static final CharSequenceTranslator ESCAPE =
new LookupTranslator(
new String[][] {
{"\"", "\\\""},
{"\\", "\\\\"},
});
public MyStringHttpMessageConverter() {
super();
}
public MyStringHttpMessageConverter(Charset defaultCharset) {
super(defaultCharset);
}
@Override
protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException {
//输出json字符串双引号转义
String target = ESCAPE.translate(str);
super.writeInternal(target, outputMessage);
}
}