Restful接口400错误 Illegal unquoted character ((CTRL-CHAR, code 19)): has to be escaped using backslash to be included in string value 解决
此错误与restful框架的json序列化配置有关,例如使用fastjson 可以修改框架默认json解析配置
如jersey框架 增加如下配置:
package com.chanjet.finance.Provider;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
@Provider
public class ObjectMapperProvider implements ContextResolver<ObjectMapper>
{
final ObjectMapper defaultObjectMapper;
public ObjectMapperProvider()
{
defaultObjectMapper = createDefaultMapper();
}
@Override
public ObjectMapper getContext(final Class<?> type)
{
return defaultObjectMapper;
}
private static ObjectMapper createDefaultMapper()
{
//覆盖jersey默认 json 解析器
//增加对允许UNQUOTED_CONTROL_CHARS字符
final ObjectMapper result = new ObjectMapper()
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
result.enable(SerializationFeature.INDENT_OUTPUT);
return result;
}
}
//覆盖框架默认的解析器
register(ObjectMapperProvider.class);
// Jackson JSON marshalling
register(JacksonFeature.class);
fastjson其他序列话属性:
AUTO_CLOSE_SOURCE(true),
ALLOW_COMMENTS(false),
ALLOW_YAML_COMMENTS(false),
ALLOW_UNQUOTED_FIELD_NAMES(false),
ALLOW_SINGLE_QUOTES(false),
ALLOW_UNQUOTED_CONTROL_CHARS(false),
ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER(false),
ALLOW_NUMERIC_LEADING_ZEROS(false),
ALLOW_NON_NUMERIC_NUMBERS(false),
STRICT_DUPLICATE_DETECTION(false);