我有一个带有 Spring Boot 和 Jetty 的简单应用程序。我有一个简单的控制器返回一个具有 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 转储,如下所示:
"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
.
为什么杰克逊在 Spring Boot 中表现得这样?
** 更新 **
对于那些寻找完整的分步指南如何解决这个问题的人,我在提出问题后发现了这一点:http: //lewandowski.io/2016/02/formatting-java-time-with-spring-boot-using-json/