0

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?

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Have you checked this already https://stackoverflow.com/questions/27952472/serialize-deserialize-java-8-java-time-with-jackson-json-mapper ? – cool Mar 22 '18 at 00:03
  • @cool yes. That post's answer simply says, if you use Jackson 310 you don't need custom deserializers. I don't have any custom deserializers – Don Rhummy Mar 22 '18 at 01:00

1 Answers1

0

Include This dependency for json deserializers:

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
Sanjay
  • 2,481
  • 1
  • 13
  • 28