从端点调用时,Spring Boot 获取 UTC+1 而不是 GMT

Posted

技术标签:

【中文标题】从端点调用时,Spring Boot 获取 UTC+1 而不是 GMT【英文标题】:Springboot getting UTC+1 instead of GMT when called from endpoint 【发布时间】:2020-06-02 10:17:10 【问题描述】:

实际上,当从端点调用日期时,我遇到了问题:

返回的日期是 UTC+1

一小时

但在数据库中日期是正确的 (UTC+1)

并且当从我的对象中调试日期也是正确的(UTC+1)

我有以下设置:

Main.java :

@SpringBootApplication
@Slf4j
public class Main extends SpringBootServletInitializer

  @PostConstruct
  void started() 
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Paris"));
  


我已将休眠设置为以下时区:

application.properties:

spring.jpa.properties.hibernate.jdbc.time_zone = Europe/Paris

我在发布之前得到了正确的日期:

数据库中的日期(由于程序处于暂停状态,需要 4 分钟才能发布):

调用端点时来自邮递员的日期

任何人都知道为什么通过端点返回的日期似乎是格林威治标准时间?

【问题讨论】:

你用什么来创建响应,杰克逊也许?如果是这样,请尝试添加日期格式 谢谢先生,它解决了我的问题:) 【参考方案1】:

vickirk 的评论指出问题与杰克逊有关

您需要在 JsonFormat 注释中添加时区 像这样:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss", timezone = "Europe/Paris")
@Column(name = "date_creation", nullable = true)
protected Date dateCreation;

相关问题可以在这里找到: https://***.com/a/31838669/5186678

【讨论】:

以上是关于从端点调用时,Spring Boot 获取 UTC+1 而不是 GMT的主要内容,如果未能解决你的问题,请参考以下文章