OffsetDateTime 始终转换为 UTC

huangapple go评论71阅读模式
英文:

OffsetDateTime always converted to UTC

问题

我有一个非常简单的Spring Boot应用程序,用于获取并返回时间戳。

以下是代码:

Controller(控制器)

@RestController
public class DemoController {

    @PostMapping("/")
    public Model test(@RequestBody Model model) {
        return model;
    }
}

Model(模型)

public class Model {
    public OffsetDateTime timestamp;
}

我注意到,当我发送非UTC时区的时间时,接收到的对象会被转换为UTC时间 - 例如,以下调用:

{
    "timestamp": "2017-07-21T17:32:28+01:00"
}

会得到这样的响应:

{
    "timestamp": "2017-07-21T16:32:28Z"
}

有没有办法禁用这种行为,以接收原样发送的时间?

英文:

I'm having a very simple spring boot app that gets and returns a timestamp.

The code is as follow:

Controller

@RestController
public class DemoController {

    @PostMapping("/")
    public Model test(@RequestBody Model model) {
        return model;
    }
}

Model

public class Model {
    public OffsetDateTime timestamp;
}

I've noticed that when I'm sending timezones which are not UTC the object I'm receiving converted into UTC - for example, the following call:

{
    "timestamp": "2017-07-21T17:32:28+01:00"
}

has this response:

{
    "timestamp": "2017-07-21T16:32:28Z"
}

Is there a way to disable this behavior and receive the time as it was send?

答案1

得分: 3

这是因为Jackson在反序列化时使用了上下文默认的时区。在Spring Boot中,您可以通过添加以下配置来轻松禁用它:

spring.jackson.deserialization.adjust-dates-to-context-time-zone=false

到您的application.properties文件中。

英文:

This happens because Jackson is using context default timezone when deserializing.
In Spring-Boot you can disable this quite easily, by just adding:

spring.jackson.deserialization.adjust-dates-to-context-time-zone=false

to your application.properties.

huangapple
  • 本文由 发表于 2020年8月5日 00:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定