2 回答

TA贡献2080条经验 获得超4个赞
使用实用程序类将模块注册到ObjectMapper实例。
public static ObjectMapper newObjectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
return configureObjectMapper(objectMapper);
}
然后在实际代码和测试用例中使用此方法以避免不同的行为。ObjectMapper这样,无论是测试代码还是实际代码,您都可以在应用程序的任何位置保持一致。
public static ObjectMapper configureObjectMapper(final ObjectMapper objectMapper) {
return objectMapper
.registerModule(new LocaleModule())
.registerModule(new DateTimeDeserializationModule())
.registerModule(new DateTimeSerializationModule())
.registerModule(new JavaMoneyModule())
.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

TA贡献1772条经验 获得超6个赞
作为第一次下注,您可以致电:
mapper.registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule());
“查找”部分实际上使用 ServiceLoader 机制,并且正如文档所述,这被认为可能是昂贵的
添加回答
举报