I'm using Spring Boot 1.5.4, Spring Data REST, HATEOAS, Hibernate 5.2.10Final. I'm exposing repositories with Spring Data REST and it works fine.
我正在使用Spring Boot 1.5.4,Spring Data REST,HATEOAS,Hibernate 5.2.10Final。我正在使用Spring Data REST公开存储库,它工作正常。
My model beans extends this class:
我的模型bean扩展了这个类:
@TypeDefs({ @TypeDef(name = "json", typeClass = JsonStringType.class), @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) })
@EntityListeners({ AuditingEntityListener.class })
@MappedSuperclass
@Audited
public abstract class AbstractEntity extends AbstractPersistable<Long> {
private static final long serialVersionUID = 1L;
/* "UUID" and "UID" are Oracle reserved keywords -> "sid" */
@Column(name = "sid", unique = true, nullable = false, updatable = false, length = 36)
private String sid;
@CreatedBy
private String createdBy;
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
@LastModifiedBy
private String lastModifiedBy;
// Trick to start version counting from 1 instead of 0
@Version
private long version = 1;
Like you see, I'm using JDK 8 LocalDateTime. In my pom.xml I'm using also:
就像你看到的那样,我正在使用JDK 8 LocalDateTime。在我的pom.xml中,我也在使用:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
In fact the REST reply is serialized in the right way:
事实上,REST回复是以正确的方式序列化的:
{
"sid": "2b2530a4-6cc7-41a0-a7b5-9bf0466223b4",
"createdDate": "2017-06-30T19:12:44",
"lastModifiedDate": "2017-06-30T19:12:44",
"lastModifiedBy": null,
"name": "Administrator",
"landlinePhone": null,
"mobilePhone": null,
"username": "admin",
"email": "admin@email.com",
"timeZone": "Europe/Rome",
"cashFund": 0,
"enabled": true,
"roles": [
"Amministratore"
],
"activeWorkSession": null,
"new": false
}
I'm storing date time in UTC format in the database thanks to these properties and it works fine:
由于这些属性,我将日期时间以UTC格式存储在数据库中,并且工作正常:
spring.datasource.url=jdbc:mysql://localhost:3306/buslet?useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=true
spring.jpa.hibernate.jdbc.time_zone = UTC
I want that my REST endpoints return date time in UTC but unfortunately it doesn't work. I've to specify that my JDK is using system default locale (Europe/Rome) and I set these property that should do the trick...
我希望我的REST端点以UTC格式返回日期时间,但遗憾的是它不起作用。我要指定我的JDK使用系统默认语言环境(欧洲/罗马),并设置这些属性应该做的诀窍......
spring.jackson.time-zone=UTC
spring.jackson.deserialization.adjust_dates_to_context_time_zone=false
Unfortunately it doesn't work. Am I missing something?
不幸的是它不起作用。我错过了什么吗?
1 个解决方案
#1
0
I used com.fasterxml.jackson.annotation.JsonFormat;
我用过com.fasterxml.jackson.annotation.JsonFormat;
package from below dependency jar try with
包从下面的依赖jar尝试
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
Annotate class property with below annotation
使用以下注释注释类属性
@JsonFormat(shape = JsonFormat.Shape.STRING ,pattern = "dd-MM-YYYY" , timezone="UTC") private Date from_date;
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern =“dd-MM-YYYY”,timezone =“UTC”)private Date from_date;
#1
0
I used com.fasterxml.jackson.annotation.JsonFormat;
我用过com.fasterxml.jackson.annotation.JsonFormat;
package from below dependency jar try with
包从下面的依赖jar尝试
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
Annotate class property with below annotation
使用以下注释注释类属性
@JsonFormat(shape = JsonFormat.Shape.STRING ,pattern = "dd-MM-YYYY" , timezone="UTC") private Date from_date;
@JsonFormat(shape = JsonFormat.Shape.STRING,pattern =“dd-MM-YYYY”,timezone =“UTC”)private Date from_date;