From c60e01034dd97b65c7908c5aec8baec2bbf4ce75 Mon Sep 17 00:00:00 2001 From: mikr Date: Sun, 28 Feb 2021 22:50:54 +0100 Subject: [PATCH] JAVA-4215 Fix issue in "Jackson Bidirectional Rels" article --- .../jackson/bidirection/ItemWithRef.java | 4 +- .../jackson/bidirection/UserWithRef.java | 6 +-- .../JacksonBidirectionRelationUnitTest.java | 37 ++++++++++++++++--- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithRef.java b/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithRef.java index 0ca8d721e8..1bd2451c6c 100644 --- a/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithRef.java +++ b/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/ItemWithRef.java @@ -1,12 +1,12 @@ package com.baeldung.jackson.bidirection; -import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.fasterxml.jackson.annotation.JsonBackReference; public class ItemWithRef { public int id; public String itemName; - @JsonManagedReference + @JsonBackReference public UserWithRef owner; public ItemWithRef() { diff --git a/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithRef.java b/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithRef.java index 3de03fc651..cc7b2b52ca 100644 --- a/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithRef.java +++ b/jackson-modules/jackson-annotations/src/main/java/com/baeldung/jackson/bidirection/UserWithRef.java @@ -3,13 +3,13 @@ package com.baeldung.jackson.bidirection; import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonManagedReference; public class UserWithRef { public int id; public String name; - @JsonBackReference + @JsonManagedReference public List userItems; public UserWithRef() { @@ -19,7 +19,7 @@ public class UserWithRef { public UserWithRef(final int id, final String name) { this.id = id; this.name = name; - userItems = new ArrayList(); + userItems = new ArrayList<>(); } public void addItem(final ItemWithRef item) { diff --git a/jackson-modules/jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java b/jackson-modules/jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java index d0c5209891..b3ce27ad94 100644 --- a/jackson-modules/jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java +++ b/jackson-modules/jackson-annotations/src/test/java/com/baeldung/jackson/bidirection/JacksonBidirectionRelationUnitTest.java @@ -1,7 +1,9 @@ package com.baeldung.jackson.bidirection; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @@ -16,7 +18,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonBidirectionRelationUnitTest { - @Test(expected = JsonMappingException.class) + @Test (expected = JsonMappingException.class) public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException { final User user = new User(1, "John"); final Item item = new Item(2, "book", user); @@ -26,16 +28,39 @@ public class JacksonBidirectionRelationUnitTest { } @Test - public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException { + public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithSerialization_thenCorrect() throws JsonProcessingException { final UserWithRef user = new UserWithRef(1, "John"); final ItemWithRef item = new ItemWithRef(2, "book", user); user.addItem(item); - final String result = new ObjectMapper().writeValueAsString(item); + final String itemJson = new ObjectMapper().writeValueAsString(item); + final String userJson = new ObjectMapper().writeValueAsString(user); - assertThat(result, containsString("book")); - assertThat(result, containsString("John")); - assertThat(result, not(containsString("userItems"))); + assertThat(itemJson, containsString("book")); + assertThat(itemJson, not(containsString("John"))); + + assertThat(userJson, containsString("John")); + assertThat(userJson, containsString("userItems")); + assertThat(userJson, containsString("book")); + } + + @Test + public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithDeserialization_thenCorrect() throws JsonProcessingException { + final UserWithRef user = new UserWithRef(1, "John"); + final ItemWithRef item = new ItemWithRef(2, "book", user); + user.addItem(item); + + final String itemJson = new ObjectMapper().writeValueAsString(item); + final String userJson = new ObjectMapper().writeValueAsString(user); + + final ItemWithRef itemRead = new ObjectMapper().readValue(itemJson, ItemWithRef.class); + final UserWithRef userRead = new ObjectMapper().readValue(userJson, UserWithRef.class); + + assertThat(itemRead.itemName, is("book")); + assertThat(itemRead.owner, nullValue()); + + assertThat(userRead.name, is("John")); + assertThat(userRead.userItems.get(0).itemName, is("book")); } @Test