LocalDateTime serialization in a Spring Boot HATEOAS rest service

Viewed 942

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.

1 Answers
Related