Spring Data(Rest):日期序列化映射返回错误的值

时间:2016-02-22 19:43:03

标签: java spring postgresql spring-data-jpa spring-data-rest

我使用Spring Boot和Data Rest在Java8中创建一个简单的微服务,并在我的JSON响应中的Date属性中获取错误的序列化值。

我的实体:

@Entity
public class ArchivedInvoice implements Serializable {
    ...
    @Column
    private java.util.Date invoiceDate;
    ...
}

我的存储库界面:

@RepositoryRestResource(collectionResourceRel = "archivedinvoices", path = "archivedinvoices")
public interface ArchivedInvoiceRepository extends PagingAndSortingRepository < ArchivedInvoice, Long > {
    ...
        @RestResource(rel = "findByDate", path = "findByDate")
        public Page< ArchivedInvoice > findByInvoiceDate(@Param("invoiceDate") @Nullable @DateTimeFormat(iso = ISO.DATE) Date invoiceDate, Pageable pageable);
    ...
}

Postgres将属性保存在一个简单的日期(invoice_date日期NOT NULL - '2016-02-22'),但JSON响应返回:

  "invoiceDate" : "2016-02-21T23:00:00.000+0000"

我该如何避免这种情况?

1 个答案:

答案 0 :(得分:3)

java.util.Date实际上是一个时间戳:

  

类Date表示特定的时刻,以毫秒为单位   精度。

如果SQL类型是日期,请使用java.sql.Date。 或者,如果您使用java 8,则可以尝试使用java.time.LocalDate。为此,您需要register Springs JSR310 JPA converters