jdk8时间序列化配置#
Copy@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper serializingObjectMapper() {
JavaTimeModule javaTimeModule = new JavaTimeModule();
/** 序列化配置,针对java8 时间 **/
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
/** 反序列化配置,针对java8 时间 **/
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
return Jackson2ObjectMapperBuilder.json().modules(javaTimeModule).build();
}
}
另个写法,时间和double类型序列化配置#
Copy@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper serializingObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
// 设置时区
objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
// 设置时间格式
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
JavaTimeModule javaTimeModule = new JavaTimeModule();
/** 序列化配置,针对java8 时间 **/
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
/** 反序列化配置,针对java8 时间 **/
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
/** 声明自定义模块,配置double类型序列化配置 **/
SimpleModule module = new SimpleModule("DoubleSerializer", new Version(1, 0, 0, ""));
// 注意Double和double需要分配配置
module.addSerializer(Double.class, new DoubleSerializer());
module.addSerializer(double.class, new DoubleSerializer());
/** 注册模块 **/
objectMapper
.registerModule(javaTimeModule)
.registerModule(module)
.registerModule(new Jdk8Module())
.registerModule(new ParameterNamesModule());
return objectMapper;
}
}
public class DoubleSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
BigDecimal d = new BigDecimal(value.toString());
gen.writeNumber(d.stripTrailingZeros().toPlainString());
}
@Override
public Class<Double> handledType() {
return Double.class;
}
}
关于解决double类型科学计数法的问题,还可以做如下配置,不需要自己写DoubleSerializer
CopyobjectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
标签:jackson,ofPattern,dd,计数法,javaTimeModule,DateTimeFormatter,new,序列化,class
From: https://blog.51cto.com/u_16110904/6534786