Jackson在Spring Boot中错误地序列化了ZonedDateTime [英] Jackson serializes a ZonedDateTime wrongly in Spring Boot

查看:322
本文介绍了Jackson在Spring Boot中错误地序列化了ZonedDateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring Boot和Jetty的简单应用程序。我有一个简单的控制器返回一个对象,它有一个Java 8 ZonedDateTime

I have a simple application with Spring Boot and Jetty. I have a simple controller returning an object which has a Java 8 ZonedDateTime:

public class Device {
  // ...
  private ZonedDateTime lastUpdated;

  public Device(String id, ZonedDateTime lastUpdated, int course, double latitude, double longitude) {
    // ...
    this.lastUpdated = lastUpdated;
    // ...
  }

  public ZonedDateTime getLastUpdated() {
    return lastUpdated;
  }
}

在我的 RestController中我只是:

@RequestMapping("/devices/")
public @ResponseBody List<Device> index() {
  List<Device> devices = new ArrayList<>();
  devices.add(new Device("321421521", ZonedDateTime.now(), 0, 39.89011333, 24.438176666));

  return devices;
}

我原以为 ZonedDateTime 要根据ISO格式进行格式化,但我得到的是类的整个JSON转储:

I was expecting the ZonedDateTime to be formatted according to the ISO format, but instead I am getting a whole JSON dump of the class like this:

"lastUpdated":{"offset":{"totalSeconds":7200,"id":"+02:00","rules":{"fixedOffset":true,"transitionRules":[],"transitions":[]}},"zone":{"id":"Europe/Berlin","rules":{"fixedOffset":false,"transitionRules":[{"month":"MARCH","timeDefinition":"UTC","standardOffset":{"totalSeconds":3600,"id":"+01:00","rules":{"fixedOffset":true,"transitionRules":[],"transitions":[]}},"offsetBefore":{"totalSeconds":3600,"id":"+01:00","rules":{"fixedOffset":true,"transitionRules":[],"transitions":[]}},"offsetAfter":{"totalSeconds":7200,"id":"+02:00", ...

我只有一个 spring-boot-starter-web 应用程序,使用 spring-boot-starter-jetty 并且不包括 spring-boot-starter-tomcat

I just have a spring-boot-starter-web application, using spring-boot-starter-jetty and excluding spring-boot-starter-tomcat.

为什么Jackson行为在Spring Boot中这样吗?

Why is Jackson behaving like this in Spring Boot?

**更新**

对于那些寻求完整步骤的人步骤指南如何解决这个问题后我发现了这个问题:
http://lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/

For those looking for a full step by step guide how to solve this I found this after asking the question: http://lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/

推荐答案

有一个图书馆 jackson-datatype-jsr310 。试试吧。

There is a library jackson-datatype-jsr310. Try it.

这个库涵盖了新的日期时间API,并且还包括 ZonedDateTime 的序列化器。

This library covers new datetime API and includes serializers for ZonedDateTime too.

您只需要添加 JavaTimeModule

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

更新

将日期时间转换为 ISO-8601 字符串你应该禁用 WRITE_DATES_AS_TIMESTAMPS 功能。您可以通过覆盖 ObjectMapper bean或使用应用程序属性

To convert datetime to ISO-8601 string you should disable WRITE_DATES_AS_TIMESTAMPS feature. You can easily do by either overriding ObjectMapper bean or by using application properties:

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false

这篇关于Jackson在Spring Boot中错误地序列化了ZonedDateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆