英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论