0

I have String as below,

value = {"name":"John","timeStamp":"2020-08-11T13:31:31"}

My Pojo class is,

@Getter
@ToString
@AllArgsConstructor
@NoArgsConstructor
class Person{
    
    private String name;    

    @JsonFormat(pattern = "YYYY-MM-dd HH:mm", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime timeStamp; 
}

I use the below artifact, as per https://github.com/FasterXML/jackson-modules-java8

 <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.11.1</version>
        </dependency>

I use the below config,

 final ObjectMapper objectMapper = JsonMapper.builder()
                        .addModule(new ParameterNamesModule())
                        .addModule(new Jdk8Module())
                        .addModule(new JavaTimeModule())
                        .build();

objectMapper.readValue(value, Person.class)

I'm getting the below exception,

Caused by: java.io.NotSerializableException: java.time.format.DateTimeFormatter

I tried with the below in the timeStamp also, but no luck

 @JsonFormat(pattern = "yyy-MM-ddThh:mm:ss")
prostý člověk
  • 909
  • 11
  • 29

1 Answers1

1

The ISO format you're showing is the default so no need to get complicated and it should work!

class Person {
    private String name;    
    private LocalDateTime timeStamp; 
}

PS. suggest you use jackson-bom managed dependencies to avoid 2.11.1 appearing all over the place. See How to use BOM file with Maven? for a BOM example.

drekbour
  • 2,895
  • 18
  • 28
  • thank you very much for your reply. I tired with standalone program with my existing code, works good. Due to other API call, throws me an error. – prostý člověk Aug 11 '20 at 13:13