3

I'm facing the following problem: in a project using Spring Boot, spring-data-jpa and spring-data-rest, to publish HATEOAS rest services, I would like to convert a LocalDateTime variable to something like "2014-12-20T02:30:00.472" for serialization purpose, that is I want the response sent to my client always contains that format.

Following this and that suggestions, I used these annotations in my model class:

public class Order {
    ...
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS")
    private LocalDateTime createdAt;
    ...
    getter and setter
}

and this is my Repository interface:

@RepositoryRestResource(path = "orders", collectionResourceRel = "orders")
public interface IOrderRepository extends JpaRepository<Order, Long> {

}

I also have a controller to test these stuffs:

@RestController
public class JavaTimeController {

    IOrderRepository repo;
    public JavaTimeController(IOrderRepository repo) {
        super();
        this.repo = repo;
    }

    @RequestMapping("/dblocaldatetime")
    public Order dbLocalDateTime() {
        Order order = repo.findOne(1L);
        return order; 
    }
}

Now the weird thing:

  • if I send a request to the URL "http://localhost:8080/dblocaldatetime", then my test controller retrive the order with id=1 and the correct datetime format will be shown (i.e. "2014-12-20T02:30:00.472"); it is worth to notice that in this case the resource is returned in a "non-HATEOAS" form, i.e. it doesn't have any "_link" or "_embedded" decoration
  • querying the rest URI "http://localhost:8080/orders/1" I receive a HATEOAS response, but this time the datetime format is something like this: "createdAt" : { "year" : 2010, "month" : "JANUARY", "dayOfMonth" : 1, "dayOfWeek" : "FRIDAY", "dayOfYear" : 1, "monthValue" : 1, "hour" : 2, "minute" : 2, "second" : 0, "nano" : 0, "chronology" : { "id" : "ISO", "calendarType" : "iso8601" } }

Why is this happening? I think something happens during the process of building the HATEOAS response, but I'm unable to investigate further: how I could? Any help will be appreciated.

Spuches
  • 51
  • 1
  • 5
  • I'd suggest using [Instant instead of LocalDateTime](https://stackoverflow.com/a/43201932/5873923). Also, don't forget to add the jackson-datatype-jsr310 dependency in your gradle/maven configuration file. – Marc Tarin May 27 '17 at 11:24
  • Thank you @mark, for your answer. I've already added the jackson-datatype-jsr310 dependency, so I'm able to get the format I want, at least in some cases, i.e. when no HATEOAS response is built. On the other case, when the response is produced by the IOrderRepository, then it seems that spring doesn't take into account the need for conversion. – Spuches May 28 '17 at 05:40
  • In my [project](https://github.com/Cepr0/restvotes) it was enough to use annotation @JsonFormat for field and add the jackson-datatype-jsr310 as dependency. See my [answer](https://stackoverflow.com/a/41324142/5380322)... – Cepr0 May 28 '17 at 07:57

1 Answers1

2

In the end, I found what the problem was: it was simply, a dirty-browser-cache problem; for some reason while changing different settings I always got the same result and it seemed that there was a problem with HATEOAS; cleaning the browser cache solved the problem.

Following @Cepr0 suggestion, also found that using only @JsonFormat annotation is enough; so non need for @JsonSerialize(using = LocalDateTimeSerializer.class) and @JsonDeserialize(using = LocalDateTimeDeserializer.class) annotations.

Spuches
  • 51
  • 1
  • 5