简体   繁体   English

spring开机调用API返回错误时间

[英]Return incorrect time when call API in spring boot

my mapping code here:我的映射代码在这里:

@Column(name = "movie_name", nullable = false, length = 100)
private String name;

@Column(name = "director", nullable = false, length = 50)
private String director;

@Column(name = "cast", nullable = false, length = 255)
private String cast;

@Column(name = "duration_start", nullable = false)
@CreationTimestamp
@Temporal(TemporalType.TIME)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss", timezone = "Asia/Vietnam")
private Date durationStart;

@Column(name = "duration_end", nullable = false)
@CreationTimestamp
@Temporal(TemporalType.TIME)
private Date durationEnd;

@Column(name = "genre", nullable = false)
private String genre;

@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "release_date", nullable = false, length = 50)
private Date releaseDate;

@Column(name = "language", nullable = false, length = 100)
private String language;

@Column(name = "rated", nullable = false, length = 100)
private String rate;

@Column(name = "poster", length = 1000, nullable =  false)
private String poster;

@OneToMany(mappedBy = "movie")
private List<Ticket> listTickets;

}` }`

i have data time like that image:我有像那个图像那样的数据时间:

but when i call api it return like here:但是当我打电话给 api 时,它会像这里一样返回: it is seem that the time is automatically plus 8 hours when i call api (int duration_start)当我拨打 api (int duration_start) 时,时间似乎自动加了 8 小时

i am trying to add annotation @jsonformat and fix server utc in application.properpties but it is seem that it is not effective我正在尝试在 application.properpties 中添加注释 @jsonformat 并修复服务器 utc,但它似乎无效

It may be that the time zone is inconsistent with the spring json constructor you are using.可能是你使用的spring json构造函数时区不一致。 First, you can print the time obtained by the backend to see if it is the same as the frontend.首先可以把后台获取到的时间打印出来,看是否和前台一致。 If not, you need to configure the springboot file.如果没有,则需要配置springboot文件。 add content:添加内容:

spring:
  jackson:
    time-zone: (Your Timezone)
    date-format: yyyy-MM-dd HH:mm:ss

If the two are consistent, you need to check whether the time stored in the database is the same as the correct time.如果两者一致,则需要检查数据库中存储的时间是否与正确的时间相同。 If there is a gap between the time of the database and the actual time, you need to edit the file in the spring database:如果数据库时间和实际时间有差距,需要编辑spring数据库中的文件:

spring.datasource.url=jdbc:mysql://11.1.1.1111:3306/database?characterEncoding=utf-8&serverTimezone=GMT%2B8

Database configuration followed by数据库配置后跟

 &serverTimezone=GMT%2B8

("%2B" means "+","%2D" means "-",choose your Timezone) (“%2B”表示“+”,“%2D”表示“-”,选择您的时区)

You can also check whether the time zone of the database is correct.您还可以检查数据库的时区是否正确。 If not, you can set the database time zone (but not recommended, it will affect the old data)如果没有,可以设置数据库时区(但不推荐,会影响旧数据)

set global time_zone = '+7:00';

set time_zone = '+7:00';

Or add it in springboot's application.properties或者在springboot的application.properties中添加

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone= GMT+7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM