【问题标题】:Reading data using Spring data jpa automatically converting timestamp to current timezone使用 Spring data jpa 读取数据自动将时间戳转换为当前时区
【发布时间】:2020-09-17 03:25:21
【问题描述】:

我有一个表 User 有列

user_id, first_name,last_name, created_time(timestamp)

我有一个类用户实体

@Getter
@Setter
@Table(name="user")
@Entity
public class User {
   @Id
   private Long userId;
   @Column(name="first_name")
   private String firstName;
   @Column(name="last_name")
   private String lastName;
   @Column(name="created_time")
   private Timestamp createdTime;
}

我有一个界面用户存储库

public interface UserRepository extends CRUDRepository<User,Long> {
   User findByUserId(Long id);
}

存储在数据库表中的 created_time 是 2020-09-08 15:38:13,当我使用 spring data jpa 读取对象时,它返回为“2020-09-08 21:08: 13"

我应该如何确保这种自动时区转换不会发生?

【问题讨论】:

    标签: mysql spring-boot spring-data-jpa


    【解决方案1】:

    问题的根本原因是 Jackson 自动将时间戳值转换为 UTC,然后将其序列化。

    为了纠正这种行为,您可以将以下属性添加到您的 application.properties 并指定与您的数据库服务器使用的时区值相同的时区值。

    spring.jackson.time-zone=Asia/Kolkata
    

    【讨论】:

    • 我对一个虚拟项目进行了同样的尝试,它对我有用。您能否分享所涉及的不同组件的版本。
    • Mysql 5.7.3,Spring boot 2.3.0.RELEASE,mysql驱动8.0.20
    【解决方案2】:

    article解释了这个问题,也提出了解决方案。

    你也可以看看this的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2014-05-22
      • 2019-11-12
      • 2019-03-17
      • 1970-01-01
      • 2011-05-10
      相关资源
      最近更新 更多