简体   繁体   中英

Get error “Can not deserialize value of type java.time.LocalDateTime from String” with jackson 310 + Spring boot

None of the strings I use in JSON are able to be deserialized into a LocalDateTime with Spring Boot and Jackson 310. I tried all of the following:

  • 2017-05-04T11:28:56.816
  • 2014-01-01T00:00:00
  • 2017-09-29 00:00:00

But none work. My DTO is simple and I tried:

@ApiModel
@Validated
public class DateRange implements Serializable
{
    @ApiModelProperty
    @NotNull
    @JsonFormat( shape = JsonFormat.Shape.ANY, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT" )
    protected LocalDateTime start;

    @ApiModelProperty
    @NotNull
    @JsonFormat( shape = JsonFormat.Shape.ANY, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT" )
    protected LocalDateTime end;

    public DateRange()
    {
    }

    public LocalDateTime getStart()
    {
        return start;
    }

    public void setStart(LocalDateTime start)
    {
        this.start = start;
    }

    public LocalDateTime getEnd()
    {
        return end;
    }

    public void setEnd(LocalDateTime end)
    {
        this.end = end;
    }

    @Override
    public boolean equals(Object o)
    {
        if ( this == o )
        {
            return true;
        }
        if ( o == null || getClass() != o.getClass() )
        {
            return false;
        }
        DateRange range = ( DateRange ) o;
        return Objects.equals( start, range.start ) && Objects.equals( end, range.end );
    }

    @Override
    public int hashCode()
    {

        return Objects.hash( start, end );
    }
}

The JSON I submitted was:

{
    "start": "2017-09-29 00:00:00",
    "end": "2017-09-29  00:00:00"
}

I also tried all the diff versions posted up above in the bulleted list.

What am I doing wrong?

Include This dependency for json deserializers:

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM