jackson jdk版本对应关系_SpringBoot - jackson 序列化配置,支持jdk8 时间类型和解决科学计数法...

jdk8时间序列化配置

@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类型序列化配置

@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 {

@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 handledType() {

return Double.class;

}

}

关于解决double类型科学计数法的问题,还可以做如下配置,不需要自己写DoubleSerializer

objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值