Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 8 date/time type java.time.LocalDateTime not supported by default #3164

Closed
esfomeado opened this issue May 25, 2021 · 14 comments
Closed

Comments

@esfomeado
Copy link

esfomeado commented May 25, 2021

Describe the bug
I have upgrade Jackson from 2.11.4 to 2.12.3 and not I get this exception:

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

Now I have to explicitly register the JavaTimeModule(). Is there any behavior change?

Thanks

Version information
2.12.3

To Reproduce
https://github.com/esfomeado/jackson-test

Java 8

Expected behavior
Should work the same way has the previous version.

Additional context
Spring Boot 2.5

@esfomeado esfomeado added the to-evaluate Issue that has been received but not yet evaluated label May 25, 2021
@cowtowncoder
Copy link
Member

This is intentional, see #2683: Java 8 date/time values should NOT be serialized as POJOs by default because that representation has caused lots of issues -- it cannot be read back.

@cowtowncoder cowtowncoder added 2.13 and removed to-evaluate Issue that has been received but not yet evaluated labels May 26, 2021
@esfomeado
Copy link
Author

Just saw as well your comment on #3091.
That should fix my problem if it goes on 2.13.

Thanks

@cowtowncoder
Copy link
Member

@esfomeado Ah, yes. Did the code sample use convertValue()? One thing #3091 could use is actual reproduction of the issue (wrt convertValue() or use with JsonNode)... to verify that fix works.

@Tuegor
Copy link

Tuegor commented Aug 22, 2022

Hey, nice chit-chat between you two, but how to fix this one after all? I've added the dependency to POM as was required, but still it's not working!

@GedMarc
Copy link

GedMarc commented Aug 22, 2022

ObjectMapper.registerModule.....

@Tuegor
Copy link

Tuegor commented Aug 22, 2022

Thanks, found this one already.

@gautamkumargojoko
Copy link

ObjectMapper.registerModule.....

I dont understand why but it is not working for me!!

@AndyCodez
Copy link

ObjectMapper.registerModule.....

I dont understand why but it is not working for me!!

@gautamkumargojoko were you able to solve this?

@gautamkumargojoko
Copy link

ObjectMapper.registerModule.....

I dont understand why but it is not working for me!!

@gautamkumargojoko were you able to solve this?

after adding this to Config file

 @Bean
  public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
    Jackson2ObjectMapperBuilder builder =
        new Jackson2ObjectMapperBuilder()
            .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .serializers(
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")))
            .serializationInclusion(JsonInclude.Include.NON_NULL);
    return new MappingJackson2HttpMessageConverter(builder.build());
  }

and added these annotations :

  @JsonSerialize(using = LocalDateTimeSerializer.class)
  @JsonDeserialize(using = LocalDateTimeDeserializer.class)
  @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
  private LocalDateTime createdAt;

then it worked for me

@cowtowncoder
Copy link
Member

It should NOT be necessary -- and I would recommend against -- add @JsonSerialize / @JsonDeserialize settings.
Instead the DateTimeModule should be registered, to avoid having to do that.

@gautamkumargojoko
Copy link

It should NOT be necessary -- and I would recommend against -- add @JsonSerialize / @JsonDeserialize settings. Instead the DateTimeModule should be registered, to avoid having to do that.

I had tried but unfortunately it did not work for me !! @cowtowncoder

@zhaishudong
Copy link

zhaishudong commented May 6, 2024

Is there any follow-up above? I also upgraded to version 2.13 and proactively registered:

@Configuration
Public class JacksonConfig{
 @Bean
 Public objectMapper objectMapper(){
  ObjectMapper objectMapper = new Object Mapper();
  objectMapper.registerModule(new JavaTimeModule());
  return objectMapper;
 }
}

But it still doesn't work and throws an exception:

Com. fasterxml. jackson. databind. exc. InvalidDefinitionException: Java 8 date/time type ` Java. time LocalDateTime ` not supported by default: add Module "com. fasterXML. jackson. datatype: jackson datatype jsr310" to enable handling (through reference chain: java. util. HashMap ["cacheTime"])

@JooHyukKim
Copy link
Member

JooHyukKim commented May 6, 2024

It should work with Java8 module registered. Try checking ur Spring configuration/bean settings. Or try de/serializing directly in 'objectMapper()' method.

@zhaishudong

@cowtowncoder
Copy link
Member

@zhaishudong This sounds like a Spring configuration problem: Jackson cannot really do anything there. You may want to ask on Spring support forums.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants