简体   繁体   English

我如何使用 REST controller GET 将 LocalDateTime 反序列化为 LocalDate,其中响应值为 LocalDateTime,DTO 中的值为 LocalDate?

[英]How i can deserialize LocalDateTime into LocalDate using a REST controller GET where value in response is LocalDateTime, value in DTO is LocalDate?

I found a lot of information about converting / deserializing LocalDate to LocalDateTime, but did not find about LocalDateTime in LocalDate, in the case when we use a REST request, where the response contains a value with the format LocalDateTime and the DTO where this value is written has LocalDate.我发现了很多关于将 LocalDate 转换/反序列化为 LocalDateTime 的信息,但在 LocalDate 中没有找到有关 LocalDateTime 的信息,在我们使用 REST 请求的情况下,其中响应包含格式为 LocalDateTime 的值和该值所在的 DTO写有LocalDate。

DTO which fill from response body从响应主体填充的 DTO

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ContractDto {
    @JsonFormat(pattern="yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @JsonProperty(value = "date")
    private LocalDate date;
    @JsonFormat(pattern="yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @JsonProperty(value = "begin_date")
    private LocalDate beginDate;
    @JsonFormat(pattern="yyyy-MM-dd", shape = JsonFormat.Shape.STRING)
    @JsonProperty(value = "end_date")
    private LocalDate endDate;
}

Example how looks by response body (some other values was deleted for best reading)示例响应正文的外观(为了最佳阅读,删除了一些其他值)

{
    "result": [
        {
            "date": "2015-11-16T00:00:00+03:00",
            "begin_date": "2015-11-16T00:00:00+03:00",
            "end_date": "2025-04-30T00:00:00+03:00"
        },
    ]
}

Error which i've deserve;D我应得的错误;D

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [ru.tfm.connector.model.dto.ResultDto<ru.tfm.transport.model.dto.connector.ContractDto>] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDate` from String "2015-11-16T00:00:00+03:00": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2015-11-16T00:00:00+03:00' could not be parsed, unparsed text found at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDate` from String "2015-11-16T00:00:00+03:00": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2015-11-16T00:00:00+03:00' could not be parsed, unparsed text found at index 10
 at [Source: (PushbackInputStream); line: 6, column: 12] (through reference chain: ru.tfm.connector.model.dto.ResultDto["result"]->java.util.ArrayList[0]->ru.tfm.transport.model.dto.connector.ContractDto["date"])] with root cause
java.time.format.DateTimeParseException: Text '2015-11-16T00:00:00+03:00' could not be parsed, unparsed text found at index 10

What i've tried:我试过的:

  1. @JsonParser annotation @JsonParser 注解
  2. @DateFormat annotation @DateFormat 注解

What i think to try:我想尝试什么:

  1. Custom converter for Jackson? Jackson 的定制转换器?
  2. Custom deserializer?自定义解串器?

Thanks a Lot!多谢!

Try to use this annotations:尝试使用此注释:

@JsonSerialize(using = LocalDateSerializer.class) 
@JsonFormat(pattern="yyyy-MM-dd")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 LocalDate 值查询 JPA LocalDateTime 字段? - How to query JPA LocalDateTime field with a LocalDate value? 如何使用 LocalDate 查询 LocalDateTime? - How to query LocalDateTime with LocalDate? 我可以从 rest 响应接收 Date 属性,作为我的应用程序中的 LocalDateTime 或 LocalDate 吗? - Can I receive a Date attribute from rest response, as a LocalDateTime or LocalDate in my application? Spring:从@Value 参数创建 LocalDate 或 LocalDateTime - Spring: create LocalDate or LocalDateTime from @Value parameter 如何指定一个字段,以便在查询时使用字段的Joda LocalDate或LocalDateTime值将其映射到同一列? - How to specify a field, so that when we query, using Joda LocalDate or LocalDateTime value for the field, it is mapped to same column? 用MOXy解组LocalDate / LocalDateTime - Unmarshalling LocalDate/LocalDateTime with MOXy 如何在Java的LocalDateTime和LocalDate上配置Jackson序列化? - How do I configure Jackson Serialization on LocalDateTime and LocalDate for Java? 使用 Spring Boot 将 json LocalDate 转换为 LocalDateTime - Convert json LocalDate to LocalDateTime using Spring boot 如何使用类 LocalDate 和 LocalDateTime 的抽象? - How to use abstraction with classes LocalDate and LocalDateTime? 如何在 JPA 查询中将 LocalDateTime 转换为 LocalDate? - How to convert LocalDateTime to LocalDate in JPA Query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM