下面列出了org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#build ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* jackson 配置
*
* @return mapper
*/
private static ObjectMapper createJackson() {
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = Jackson2ObjectMapperBuilder.json();
jackson2ObjectMapperBuilder.simpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
ObjectMapper build = jackson2ObjectMapperBuilder.build();
// 忽略空
build.setSerializationInclusion(JsonInclude.Include.NON_NULL);
// 驼峰转下划线
// build.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
// long to String
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
build.registerModule(simpleModule);
//
build.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// build.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);
return build;
}
@Bean
public ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToDisable(
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
);
builder.featuresToEnable(
SerializationFeature.WRITE_DATES_WITH_ZONE_ID,
// SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
// SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,
// DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS,
DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
);
builder.indentOutput(true);
builder.failOnEmptyBeans(false);
builder.failOnUnknownProperties(false);
// do not include null value in json to make object graph smaller
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.modules(new GeoJsonModule(), new JavaTimeModule(), new MoneyModule());
return builder.build();
}
@Bean(name = "customObjectMapper")
public ObjectMapper customObjectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.featuresToDisable(
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
);
builder.featuresToEnable(
SerializationFeature.WRITE_DATES_WITH_ZONE_ID,
// SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
// SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS,
// DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS,
DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
);
builder.indentOutput(true);
builder.failOnEmptyBeans(false);
builder.failOnUnknownProperties(false);
// do not include null value in json to make object graph smaller
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.modules(new GeoJsonModule(), new JavaTimeModule(), new MoneyModule());
return builder.build();
}
@Bean
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
builder.failOnEmptyBeans(false);
return builder.build();
}