DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY
接受单个字符的值反序列化为数组。
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true)
public class Persion{
List<String> names;
}
接受1:
{"person": {
"names": ["lo","wang"]
}
}
接受2:
{"person": {
"names": "wayne"
}
}
MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME
mapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
反序列化时,允许有未知字段
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonInclude.Include.NON_NULL
序列化的时候,不输出null值
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
setDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(dateFormat);
annotaion
AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary));
https://www.iteye.com/blog/conkeyn-2188038
public final class JsonObjectMapper extends ObjectMapper {
/** */
private static final long serialVersionUID = 4563671462132723274L;
public JsonObjectMapper() {
super();
// 从JSON到java object
// 没有匹配的属性名称时不作失败处理
this.configure(MapperFeature.AUTO_DETECT_FIELDS, true);
// 反序列化
// 禁止遇到空原始类型时抛出异常,用默认值代替。
this.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
// 禁止遇到未知(新)属性时报错,支持兼容扩展
this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
this.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
// 按时间戳格式读取日期
// this.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
this.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
this.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
this.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// 序列化
// 禁止序列化空值
this.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
this.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
this.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true);
// 按时间戳格式生成日期
// this.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
this.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, true);
this.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
// 不包含空值属性
this.setSerializationInclusion(Include.NON_EMPTY);
this.setSerializationInclusion(Include.NON_NULL);
// this.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
// 是否缩放排列输出,默认false,有些场合为了便于排版阅读则需要对输出做缩放排列
this.configure(SerializationFeature.INDENT_OUTPUT, false);
// 设置全局的时间转化
SimpleDateFormat smt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.setDateFormat(smt);
// 设置日期反序列化组件
SimpleModule dateDeserializerModule = new SimpleModule();
dateDeserializerModule.addDeserializer(Object.class, new CustomDateDeseralizer());
this.registerModule(dateDeserializerModule);
// 注意:不要开启序列化和反序列化时为对象附加@class属性(坑)
// this.enableDefaultTyping(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT, As.PROPERTY);
}
}
网友评论