drenda
drenda

Reputation: 6264

Spring spring.jackson.time-zone=UTC doesn't work

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.

My model beans extends this class:

@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:

<dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

In fact the REST reply is serialized in the right way:

{
    "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": "[email protected]",
    "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:

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...

spring.jackson.time-zone=UTC
spring.jackson.deserialization.adjust_dates_to_context_time_zone=false

Unfortunately it doesn't work. Am I missing something?

Upvotes: 2

Views: 6120

Answers (1)

Vaibs
Vaibs

Reputation: 1606

I used com.fasterxml.jackson.annotation.JsonFormat;

package from below dependency jar try with

<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;

Upvotes: 1

Related Questions