question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NPE with JsonBackReference when deserialized when using Lombok

See original GitHub issue

Hello,

I’ve a problem while trying to upgrade from Jackson 2.6.x to Jackson 2.8.1. This upgrade is linked to a change of version of spring-boot.

While everything was working in 2.6.x, The deserialization is totally broken for me in 2.7 and 2.8.1. When I try to deserialized an entity having a @JsonBackReference annotation, I have a NullPointerException.

java.lang.NullPointerException
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.constructSettableProperty(BeanDeserializerFactory.java:733)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addReferenceProperties(BeanDeserializerFactory.java:643)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:230)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:141)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:406)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:442)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:196)
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createContextual(CollectionDeserializer.java:26)
    at com.fasterxml.jackson.databind.DeserializationContext.handleSecondaryContextualization(DeserializationContext.java:680)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:444)
    at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.construct(PropertyBasedCreator.java:79)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:606)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:296)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:475)
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3890)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3785)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)
    at com.github.davinkevin.issueJackson.entity.PodcastTest.should_serialized(PodcastTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

I’ve extract the entity in a separate project to verify if the spring environment was a source of the problem, but this NPE also appear in a simple project.

You can find the sample project here : https://github.com/davinkevin/issue-jackson-deserialize-2.7

The test which causes this problem is :

    @Test
    public void should_serialized() throws IOException {
        /* Given */
        String podcastAsText = "{\"hasToBeDeleted\":true,\"cover\":{\"id\":null,\"url\":\"https://yt3.ggpht.com/-2eu-eG4PLa8/AAAAAAAAAAI/AAAAAAAAAAA/FZUzswUhAmI/s100-c-k-no-rj-c0xffffff/photo.jpg\",\"width\":200,\"height\":200},\"url\":\"https://www.youtube.com/channel/UC_yP2DpIgs5Y1uWC0T03Chw\",\"type\":\"Youtube\",\"title\":\"Joueur Du Grenier\",\"description\":\"Test de jeux à la con !\"}";

        /* When */
        Podcast podcast1 = objectMapper.readValue(podcastAsText, Podcast.class);

        /* Then */
        assertThat(podcast1).isNotNull();
    }

and you can find here the entity Podcast and Item.

I’ve searched for an issue linked to that, without success in StackOverflow and in the issue tracker.

Thanks for your help

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Serializatorcommented, Sep 8, 2016

@cowtowncoder Is there any chance that you might know a reason why a field annotated with @JsonManagedReference doesn’t get deserialized and stays null and when @JsonManagedReference isn’t used it does actually get properly deserialized?

Sorry for not providing any information on how I use it, I’m currently not on my PC.

0reactions
acaliarocommented, Mar 27, 2019

I have had the same problem.

My solution is:

remove

@AllArgsConstructor @NoArgsConstructor

and create them in code

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Lombok @NonNull null check enforcement not working ...
I have a simple case where I want to enforce null check on values found in JSON string when converting the JSON string...
Read more >
Jackson's Deserialization With Lombok - Baeldung
The test shows how we can build a Fruit using the Lombok builder API and that the serialized Java object matches the expected...
Read more >
Could not write JSON: Infinite recursion (StackOverflowError)
The problem may be due to using Lombok and Set together. Lombok creates an equals ... Use @JsonManagedReferenc and @JsonBackReference in DAO/Entity class....
Read more >
Jackson Annotations for JSON - Spring Framework Guru
The Jackson library provides annotations that you can use in POJO's to control both serialization and deserialization between POJOs and JSON.
Read more >
https://gitee.com/dromara/dante-cloud/pulls/1.patch
在序列化时,@JsonBackReference的作用相当于@JsonIgnore,此时可以没有@JsonManagedReference。 + * 但在反序列化(deserialization,即json数据转换为对象)时,如果 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found