In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章的内容主要围绕SpringBoot jackson提供对LocalDate的支持方式是什么进行讲述,文章内容清晰易懂,条理清晰,非常适合新手学习,值得大家去阅读。感兴趣的朋友可以跟随小编一起阅读吧。希望大家通过这篇文章有所收获!
jackson提供对LocalDate的支持
SpringBoot默认使用jackson来进行json格式转换,我们在配置文件中加入如下配置可以统一的改变Spring MVC返回值的日期格式类型
spring.jackson.date-format=yyyy-MM-ddspring.jackson.time-zone=GMT+8spring.jackson.serialization.write-dates-as-timestamps=false
但是这个配置对于java8新提供的日期APILocalDate、LocalDateTime等无效。
解决办法
引入依赖
com.fasterxml.jackson.datatype jackson-datatype-jsr310 2.8.9
启动主类添加如下代码
@SpringBootApplicationpublic class GroupApplication { public static void main(String[] args) { SpringApplication.run(GroupApplication.class, args); } /** * 提供jackson对LocalDate等java8提供的日期的序列化支持 */ @Bean(name = "mapperObject") public ObjectMapper getObjectMapper() { ObjectMapper om = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); 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"))); om.registerModule(javaTimeModule); return om; }}使用Jackson格式化LocalDate对象
LocalDate可以很方便的对日期进行处理,但是在返回给前端时,展示为如下的JSON结构:
{ "dayOfWeek": "FRIDAY", "month": "JANUARY", "year": 2020, "dayOfMonth": 3, "era": "CE", "dayOfYear": 3, "monthValue": 1, "chronology": { "calendarType": "iso8601", "id": "ISO" }}
前端组件就很难处理了。接下来,我们需要将此属性反序列化并从JSON字符串序列化。为此,我们可以使用@JsonDeserialize和@JsonSerialize来注释该类的LocalDate属性,时期成为前端组件需要的String格式。
实现LocalDateDeserializer和LocalDateSerializer
下面是LocalDateSerializerand LocalDateDeserializer类的定义。
public class LocalDateDeserializer extends StdDeserializer { protected LocalDateDeserializer() { super(LocalDate.class); } @Override public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException { return LocalDate.parse(parser.readValueAs(String.class)); }}public class LocalDateSerializer extends StdSerializer { public LocalDateSerializer() { super(LocalDate.class); } @Override public void serialize(LocalDate value, JsonGenerator generator, SerializerProvider provider) throws IOException { generator.writeString(value.format(DateTimeFormatter.ISO_LOCAL_DATE)); }}实体类添加注解
实体类添加如下的注解
@JsonDeserialize(using = LocalDateDeserializer.class) @JsonSerialize(using = LocalDateSerializer.class) private LocalDate date;感谢你的阅读,相信你对"SpringBoot jackson提供对LocalDate的支持方式是什么"这一问题有一定的了解,快去动手实践吧,如果想了解更多相关知识点,可以关注网站!小编会继续为大家带来更好的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.