-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
utils/src/main/java/t5750/utils/jackson/entity/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package t5750.utils.jackson.entity; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFilter; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
@JsonFilter("studentFilter") | ||
public class Student { | ||
@JsonProperty("stdName") | ||
private String name; | ||
@JsonProperty("stdAge") | ||
private Integer age; | ||
@JsonProperty("stdCollege") | ||
private String college; | ||
@JsonProperty("stdCity") | ||
private String city; | ||
|
||
public Student() { | ||
} | ||
|
||
public Student(String name, Integer age, String college, String city) { | ||
this.name = name; | ||
this.age = age; | ||
this.college = college; | ||
this.city = city; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
utils/src/test/java/t5750/utils/jackson/JacksonJsonFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package t5750.utils.jackson; | ||
|
||
import org.junit.Test; | ||
import t5750.utils.jackson.entity.Student; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter; | ||
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider; | ||
|
||
/** | ||
* | ||
*/ | ||
public class JacksonJsonFilterTest { | ||
@Test | ||
public void testJsonFilter() throws Exception { | ||
final SimpleFilterProvider filterProvider = new SimpleFilterProvider(); | ||
filterProvider.addFilter("studentFilter", SimpleBeanPropertyFilter | ||
.serializeAllExcept("stdName", "stdCity")); | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.setFilterProvider(filterProvider); | ||
final Student student = new Student("Mohit", 30, "ABCD", "Varanasi"); | ||
final String jsonData = objectMapper.writerWithDefaultPrettyPrinter() | ||
.writeValueAsString(student); | ||
System.out.println(jsonData); | ||
} | ||
} |