File tree Expand file tree Collapse file tree
jackson-bidirectional-relation
main/java/com/fd/jackson/bidirectional/model Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
5+ <modelVersion >4.0.0</modelVersion >
6+
7+ <groupId >com.fd</groupId >
8+ <artifactId >jackson-bidirectional-relation</artifactId >
9+ <version >1.0-SNAPSHOT</version >
10+
11+ <properties >
12+ <java-version >1.8</java-version >
13+ <maven .compiler.source>1.8</maven .compiler.source>
14+ <maven .compiler.target>1.8</maven .compiler.target>
15+ </properties >
16+
17+ <dependencies >
18+ <dependency >
19+ <groupId >com.fasterxml.jackson.core</groupId >
20+ <artifactId >jackson-databind</artifactId >
21+ <version >2.9.8</version >
22+ </dependency >
23+
24+ <dependency >
25+ <groupId >org.projectlombok</groupId >
26+ <artifactId >lombok</artifactId >
27+ <version >1.18.8</version >
28+ </dependency >
29+
30+ <dependency >
31+ <groupId >org.testng</groupId >
32+ <artifactId >testng</artifactId >
33+ <version >6.14.3</version >
34+ </dependency >
35+ </dependencies >
36+
37+ </project >
Original file line number Diff line number Diff line change 1+ package com .fd .jackson .bidirectional .model ;
2+
3+ import com .fasterxml .jackson .annotation .JsonManagedReference ;
4+ import lombok .AllArgsConstructor ;
5+ import lombok .Getter ;
6+ import lombok .NoArgsConstructor ;
7+ import lombok .Setter ;
8+
9+ @ Getter
10+ @ Setter
11+ @ AllArgsConstructor
12+ @ NoArgsConstructor
13+ public class Item {
14+ private int id ;
15+ private String itemName ;
16+
17+ @ JsonManagedReference
18+ private User owner ;
19+ }
Original file line number Diff line number Diff line change 1+ package com .fd .jackson .bidirectional .model ;
2+
3+ import com .fasterxml .jackson .annotation .JsonBackReference ;
4+ import lombok .Getter ;
5+ import lombok .NoArgsConstructor ;
6+ import lombok .Setter ;
7+
8+ import java .util .ArrayList ;
9+ import java .util .List ;
10+
11+ @ Getter
12+ @ Setter
13+ @ NoArgsConstructor
14+ public class User {
15+
16+ private int id ;
17+ private String name ;
18+
19+ @ JsonBackReference
20+ private List <Item > userItems = new ArrayList <>();
21+
22+ public User (int id , String name ) {
23+ this .id = id ;
24+ this .name = name ;
25+ }
26+
27+ public void addItem (Item item ) {
28+ this .userItems .add (item );
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ import com .fasterxml .jackson .core .JsonProcessingException ;
2+ import com .fasterxml .jackson .databind .ObjectMapper ;
3+ import com .fd .jackson .bidirectional .model .Item ;
4+ import com .fd .jackson .bidirectional .model .User ;
5+ import org .testng .annotations .Test ;
6+
7+ public class BidirectionalMappingTest {
8+
9+ @ Test //(expectedExceptions = JsonMappingException.class)
10+ public void givenBidirectionRelation_whenSerializingItem_thenException ()
11+ throws JsonProcessingException {
12+
13+ User user = new User (1 , "John" );
14+ Item item = new Item (2 , "book" , user );
15+ user .addItem (item );
16+
17+ System .out .println (new ObjectMapper ().writeValueAsString (item ));
18+ }
19+
20+ @ Test //(expectedExceptions = JsonMappingException.class)
21+ public void givenBidirectionRelation_whenSerializingUser_thenException ()
22+ throws JsonProcessingException {
23+
24+ User user = new User (1 , "John" );
25+ Item item = new Item (2 , "book" , user );
26+ user .addItem (item );
27+
28+ System .out .println (new ObjectMapper ().writeValueAsString (user ));
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments