I’m using lombok + Java to otimizate the application. The class has the tags @Data
, @EqualsAndHashCode(callSuper = false)
, @NoArgsConstructor
, @AllArgsConstructor
e @Builder
before signature’s class. When I run, It shows the error below:
ERROR:
org.apache.cxf.jaxrs.JAXRSRuntimeException: org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class java.time.LocalDateTime]: can not instantiate from JSON object (need to add/enable type information?) at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@25e15ce5; line: 1, column: 188] at org.apache.cxf.jaxrs.utils.JAXRSUtils.toJAXRSRuntimeException(JAXRSUtils.java:1906) at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.convertExceptionToResponseIfPossible(JAXRSInInterceptor.java:276)
CLASS:
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Person {
private LocalDateTime date;
}
I thougth this lombok’s tags are enough. Does anyone have any idea?
Advertisement
Answer
- You are using a very old Jackson version.
- To make Jackson work with Java 8 time, you need to add jackson-datatype-jsr310 dependency and register module in ObjectMapper:
objectMapper.registerModule(new JavaTimeModule());
Complete working example with current Jackson version:
Person person = new Person(LocalDateTime.now());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
String json = objectMapper.writeValueAsString(person);
Person personDeserialized = objectMapper.readValue(json, Person.class);
Dependencies:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>