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

时间:2022-10-18 20:45:46

I use Spring Boot and Data Rest to create a simple microservice in Java8 and get a wrong serialized value in a Date attribute in my JSON response.

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

My entity:

我的实体:

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

My repository interface:

我的存储库界面:

@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 saves the attribute in a simple date (invoice_date date NOT NULL - '2016-02-22') but the JSON response returns:

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

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

How can I avoid this?

我怎么能避免这个?

1 个解决方案

#1


3  

java.util.Date is actually a timestamp:

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

The class Date represents a specific instant in time, with millisecond precision.

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

Use java.sql.Date instead if the SQL type is date. Or if you use java 8, you can try using java.time.LocalDate. For that to work you will need to register Springs JSR310 JPA converters.

如果SQL类型是date,请使用java.sql.Date。或者如果您使用java 8,则可以尝试使用java.time.LocalDate。为此,您需要注册Springs JSR310 JPA转换器。

#1


3  

java.util.Date is actually a timestamp:

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

The class Date represents a specific instant in time, with millisecond precision.

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

Use java.sql.Date instead if the SQL type is date. Or if you use java 8, you can try using java.time.LocalDate. For that to work you will need to register Springs JSR310 JPA converters.

如果SQL类型是date,请使用java.sql.Date。或者如果您使用java 8,则可以尝试使用java.time.LocalDate。为此,您需要注册Springs JSR310 JPA转换器。