Merge pull request #10528 from Maiklins/JAVA-4215-Fix_issue_in_Jackson_Bidirectional_Rels_article

Java 4215 fix issue in jackson bidirectional rels article
This commit is contained in:
Dhawal Kapil 2021-03-01 19:44:03 +05:30 committed by GitHub
commit 3cc2119850
3 changed files with 36 additions and 11 deletions

View File

@ -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() {

View File

@ -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<ItemWithRef> 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<ItemWithRef>();
userItems = new ArrayList<>();
}
public void addItem(final ItemWithRef item) {

View File

@ -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