这篇“SpringBoot中Jackson日期格式化的方法”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringBoot中Jackson日期格式化的方法”文章吧。
使用 Spring Boot 时,需要使用 Jackson 处理一些 Java Time API 类型的 JSON 序列化问题,在处理一些类的字段时,可以通过直接在属性上加注解的方式来指定其格式化样式。但是,昨天同事遇到一个格式化 Map
数据的问题,这样就不能通过加注解来解决格式化样式的问题了。
在网上各种搜索,各种尝试后,终于解决了这个问题,记录一下,以备不时之需。
闲言少叙,直接上代码:
package com.diguage.demo.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import static com.fasterxml.jackson.databind.SerializationFeature.*;
import static java.time.format.DateTimeFormatter.ofPattern;
/<strong>
* 配置类
*
* @author D瓜哥 · <a href="https://www.diguage.com/" rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a>
*/
@Configuration
public class Config {
/</strong>
* 创建 ObjectMapper 对象,配置日期格式化
*
* @author D瓜哥 · <a href="https://www.diguage.com/" rel="external nofollow" rel="external nofollow" target="_blank" >https://www.diguage.com</a>
*/
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
String dateTimepattern = "yyyy-MM-dd HH:mm:ss";
String datePattern = "yyyy-MM-dd";
DateFormat dateFormat = new SimpleDateFormat(dateTimepattern);
mapper.setDateFormat(dateFormat);
mapper.configure(WRITE_DATES_AS_TIMESTAMPS, false);
mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(LocalDate.class,
new LocalDateDeserializer(ofPattern(datePattern)));
javaTimeModule.addSerializer(LocalDate.class,
new LocalDateSerializer(ofPattern(datePattern)));
javaTimeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(ofPattern(dateTimepattern)));
javaTimeModule.addSerializer(LocalDateTime.class,
new LocalDateTimeSerializer(ofPattern(dateTimepattern)));
mapper.registerModule(javaTimeModule);
return mapper;
}
}
不知道通过这种方式指定日期格式化样式后,在处理一些打格式化样式注解的字段时,会有什么样的表现?有机会测试一下。
方式一:配置文件yml中配置
spring:
jackson:
default-property-inclusion: ALWAYS
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
这样序列化后,Date类型会被格式化成配置中的格式。
方式二:配置类中配置创建JacksonConfig.java
@Configuration
public class JacksonConfig {
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return new Jackson2ObjectMapperBuilderCustomizer() {
@Override
public void customize(Jackson2ObjectMapperBuilder builder) {
builder.serializerByType(LocalDateTime.class,
new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
builder.serializerByType(LocalDate.class,
new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
builder.serializerByType(LocalTime.class,
new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.deserializerByType(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
builder.deserializerByType(LocalDate.class,
new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
builder.deserializerByType(LocalTime.class,
new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.failOnUnknownProperties(false);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
}
};
}
}
以上就是关于“SpringBoot中Jackson日期格式化的方法”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。