Add two JsonGetter examples
This commit is contained in:
		
							parent
							
								
									e02f7825a3
								
							
						
					
					
						commit
						5bd907aa01
					
				| @ -0,0 +1,33 @@ | ||||
| package com.baeldung.jacksonannotation.serialization.jsongetter; | ||||
| 
 | ||||
| 
 | ||||
| import com.baeldung.jacksonannotation.domain.Item; | ||||
| import com.baeldung.jacksonannotation.domain.Person; | ||||
| import com.fasterxml.jackson.annotation.JsonGetter; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * Source code github.com/eugenp/tutorials | ||||
|  * | ||||
|  * @author Alex Theedom www.baeldung.com | ||||
|  * @version 1.0 | ||||
|  */ | ||||
| public class Author1 extends Person { | ||||
| 
 | ||||
|     List<Item> items = new ArrayList<>(); | ||||
| 
 | ||||
|     public Author1(String firstName, String lastName) { | ||||
|         super(firstName, lastName); | ||||
|     } | ||||
| 
 | ||||
|     @JsonGetter | ||||
|     public List<Item> getItems() { | ||||
|         return items; | ||||
|     } | ||||
| 
 | ||||
|     public void setItems(List<Item> items) { | ||||
|         this.items = items; | ||||
|     } | ||||
| } | ||||
| @ -14,11 +14,11 @@ import java.util.List; | ||||
|  * @author Alex Theedom www.baeldung.com | ||||
|  * @version 1.0 | ||||
|  */ | ||||
| public class Author extends Person { | ||||
| public class Author2 extends Person { | ||||
| 
 | ||||
|     List<Item> items = new ArrayList<>(); | ||||
| 
 | ||||
|     public Author(String firstName, String lastName) { | ||||
|     public Author2(String firstName, String lastName) { | ||||
|         super(firstName, lastName); | ||||
|     } | ||||
| 
 | ||||
| @ -27,6 +27,7 @@ public class Author extends Person { | ||||
|         return items; | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     public void setItems(List<Item> items) { | ||||
|         this.items = items; | ||||
|     } | ||||
| @ -16,10 +16,33 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||
| public class JsonGetterTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenSerializingUsingJsonGetter_thenCorrect() throws JsonProcessingException { | ||||
|     public void whenSerializingUsingJsonGetter_andNoPropertyNameSet_thenCorrect() throws JsonProcessingException { | ||||
| 
 | ||||
|         // arrange | ||||
|         Author author = new Author("Alex", "Theedom"); | ||||
|         Author1 author = new Author1("Alex", "Theedom"); | ||||
| 
 | ||||
|         // act | ||||
|         String result = new ObjectMapper().writeValueAsString(author); | ||||
| 
 | ||||
|         // assert | ||||
|         assertThat(from(result).getList("items")).isNotNull(); | ||||
| 
 | ||||
|         /* | ||||
|             { | ||||
|               "firstName": "Alex", | ||||
|               "lastName": "Theedom", | ||||
|               "items": [] | ||||
|             } | ||||
|         */ | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenSerializingUsingJsonGetter_andPropertyNameSet_thenCorrect() throws JsonProcessingException { | ||||
| 
 | ||||
|         // arrange | ||||
|         Author2 author = new Author2("Alex", "Theedom"); | ||||
| 
 | ||||
|         // act | ||||
|         String result = new ObjectMapper().writeValueAsString(author); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user