反序列化问题
- 反序列化不存在的 properties 时,报错,须要注解
@JsonIgnoreProperties
- 反序列化未知的 enum 时,报错
如果不心愿报错,那么须要批改 Jackson 配置。
Jackson 配置
办法一:批改 objectMapper 的 configuration
public class JsonUtil {
private static ObjectMapper mapper;
static {mapper = new ObjectMapper();
// ...
// ignore unknown properties
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// unknown enum -> null
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
// include none null
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
}
办法二 :批改application.yaml
配置
spring:
# Jackson 配置
jackson:
# Include NON_NULL
default-property-inclusion: non_null
# 反序列化
deserialization:
# ignore unknown property
fail-on-unknown-properties: false
# unknown enum -> null
read-unknown-enum-values-as-null: true
Have a test!
example enum:
public enum RateSpan {W01("近一周"),
M01("近一个月"), M03("近三个月"), M06("近六个月"),
Y01("近一年"), Y03("近三年"), Y00("今年以来"), YGB("成立以来");
final String msg;
RateSpan(String msg) {this.msg = msg;}
public String getMsg() {return msg;}
}
class RateSpanTest {@Test void deserialize() throws JsonProcessingException {
// "Y01" -> Y01
RateSpan span = JsonUtil.loadStr("\"Y01\"", RateSpan.class);
assertEquals(span, RateSpan.Y01);
// "FUCK" -> null
span = JsonUtil.loadStr("\"FUCK\"", RateSpan.class);
assertNull(span);
}
}
More
除了反序列化为 null
,咱们还能够将 unknown enum 反序列化为一个 默认值。
只须要批改配置:
public class JsonUtil {
private static ObjectMapper mapper;
static {mapper = new ObjectMapper();
// ...
// unknown enum -> default
mapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);
}
}
而后加上@JsonEnumDefaultValue
注解,
public enum RateSpan {
// ...
// 加上 default 注解
@JsonEnumDefaultValue
Y01("近一年"), Y03("近三年"),
// ...
}
单元测试。FUCK 反序列化为 默认的 Y01:
class RateSpanTest {@Test void deserialize() throws JsonProcessingException {RateSpan span = JsonUtil.loadStr("\"Y01\"", RateSpan.class);
assertEquals(span, RateSpan.Y01);
// "FUCK" -> Y01
span = JsonUtil.loadStr("\"FUCK\"", RateSpan.class);
assertEquals(span, RateSpan.Y01);
}
}