How do I enable the JSR310 support for LocalDate using Jackson?
Asked Answered
G

3

23

I have added the JS310 dependency to Maven and refreshed the dependencies:

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

In the domain:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
LocalDate start;

However, I am receiving this error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type java.time.LocalDate not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling

Gonorrhea answered 2/11, 2021 at 14:0 Comment(0)
P
43

The error is indicative and explains you haven't registered the JavaTimeModule module like documented at datetime : to register it you can for example do in this way (or other equivalent way explained in the link I added previously, dependly from the jackson library version you are using) :

ObjectMapper mapper = JsonMapper.builder()
    .addModule(new JavaTimeModule())
    .build();
Pastorate answered 2/11, 2021 at 14:20 Comment(7)
@Gonorrhea If you are using a version 2.x before 2.9 the code is ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JavaTimeModule());, both methods with equivalent alternatives are present in the link. I'm seeing you are using 2.13 module, checks if this is the version you are currently using, because these are the methods from official documentation.Pastorate
Wow. Learned something new. Thanks.Shillyshally
@Shillyshally Glad to have helped.Pastorate
@Pastorate should this work with spring boot 2.7.x I've tried this while trying to serialize an object from elasticseaech with a Instant field and I'm getting the same errorKalb
@Kalb For spring boot there is a specific spring-boot-starter-json json starter that includes the jackson-datatype-jsr310.Pastorate
Thanks for that. Strangely enough even without adding the dependancy if I just remove any and all object mapper configs and when creating the elasticsearch client just do this ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper(objectMapper)); it works...where as ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper()); causes it to failKalb
@Kalb Glad to have helped, without exploring the source code a possible way to explain about the success or fail is that when with the import of the ObjectMapper it automatically imports all modules including the datetime module.Pastorate
A
1

java.time.LocalDate is not supported by default so we have to register the module. Add this line for registering -

DatabindCodec.mapper().registerModule(new JavaTimeModule());
Afferent answered 6/9, 2023 at 15:52 Comment(0)
P
1

finally i have solved it: use @Configuration public class JacksonConfig {

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    
    return objectMapper;
}

} and use @Autowired private ObjectMapper objectMapper;

if u create ObjectMapper objectMapper=new ObjectMapper(); inside ur method it will not work. and one more thing i have use springboot project so no jsr 310 dependency required for me

Papiamento answered 25/6 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.