Skip to content

Commit a9d16f3

Browse files
committed
jsonfilter 내용 추가
1 parent 041c614 commit a9d16f3

6 files changed

Lines changed: 130 additions & 2 deletions

File tree

‎_data/pageMap.yml‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,24 @@ index:
2828
summary: 'anyjava wiki index'
2929
parent: null
3030
url: /wiki/index
31-
updated: '2018-09-06 02:01:37 +0900'
31+
updated: '2018-09-13 08:37:20 +0900'
3232
children: []
33+
Jackson:
34+
type: wiki
35+
title: Jackson
36+
summary: 'json, jackson'
37+
parent: Java
38+
url: /wiki/Jackson
39+
updated: '2018-09-13 08:38:06 +0900'
40+
children: []
41+
Java:
42+
type: wiki
43+
title: Java
44+
summary: ""
45+
parent: index
46+
url: /wiki/Java
47+
updated: '2018-09-13 08:36:04 +0900'
48+
children: [Jackson]
3349
JPA:
3450
type: wiki
3551
title: JPA

‎_data/tagList.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
- devops
22
- docker
3+
- jackson
4+
- java
35
- jpa
6+
- 'json,'
47
- linux
58
- slf4j
69
- spring

‎_data/tagMap.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ devops:
22
- {fileName: DevOps}
33
docker:
44
- {fileName: Docker}
5+
'json,':
6+
- {fileName: Jackson}
7+
jackson:
8+
- {fileName: Jackson}
9+
java:
10+
- {fileName: Java}
511
jpa:
612
- {fileName: JPA}
713
linux:

‎_wiki/Jackson.md‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
layout : wiki
3+
title : Jackson
4+
summary : json, jackson
5+
date : 2018-09-13 08:37:20 +0900
6+
updated : 2018-09-13 08:38:06 +0900
7+
tags : json, jackson
8+
toc : true
9+
public : true
10+
parent : Java
11+
latex : false
12+
---
13+
* TOC
14+
{:toc}
15+
16+
# Json Filter
17+
18+
```
19+
import com.fasterxml.jackson.annotation.JsonFilter;
20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.ser.FilterProvider;
23+
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
24+
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
25+
import lombok.Getter;
26+
import lombok.ToString;
27+
import lombok.experimental.FieldNameConstants;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
import static org.assertj.core.api.Java6Assertions.assertThat;
32+
33+
/**
34+
* JsonFilter 를 사용해서 json 을 generate 하는 learning Test 작성
35+
* Ad-center 내부에서 사용하는 API 이고 entity 를 json 으로 변환해서 내려야 한다면, JsonFilter 를 이용해 보는것도 좋을듯.
36+
* @see <a href="http://kwonnam.pe.kr/wiki/java/jackson">권남님 블로그: Java Jackson Library </a>
37+
* @see <a href="https://github.com/Baeldung/spring-hypermedia-api/blob/master/src/main/java/com/baeldung/web/controller/NewBookController.java">Spring MVC에서 사용한 에제코드 </a>
38+
* @see <a href="https://projectlombok.org/features/experimental/FieldNameConstants">Lombok 을 이용한 필드면 사용 방법 - 매직스트링 제거 </a>
39+
*/
40+
public class JsonFilterTest {
41+
42+
public static final String TEST_JSON_FILTER = "testJsonFilter";
43+
private ObjectMapper om;
44+
45+
@Before
46+
public void setup() {
47+
om = new ObjectMapper();
48+
}
49+
50+
@Test
51+
public void jsonFilterShouldReturnJsonWithCompositionObject() throws JsonProcessingException {
52+
String expectedJson = "{\"fooField\":\"fooValue\",\"fooInt\":111,\"bar\":{\"barField\":\"barValue\"}}";
53+
54+
FilterProvider filters = new SimpleFilterProvider()
55+
.addFilter(TEST_JSON_FILTER, SimpleBeanPropertyFilter.filterOutAllExcept(Foo.FIELD_FOO_FIELD, Foo.FIELD_FOO_INT, Foo.FIELD_BAR, Bar.FIELD_BAR_FIELD));
56+
57+
om.setFilterProvider(filters);
58+
String actualJson = om.writeValueAsString(new Foo());
59+
60+
assertThat(actualJson).isEqualTo(expectedJson);
61+
}
62+
63+
}
64+
65+
@JsonFilter(JsonFilterTest.TEST_JSON_FILTER)
66+
@FieldNameConstants
67+
@Getter
68+
@ToString
69+
class Foo {
70+
private String fooField = "fooValue";
71+
private int fooInt = 111;
72+
private String fooDummy = "fooDummy";
73+
74+
private Bar bar = new Bar();
75+
}
76+
77+
@JsonFilter(JsonFilterTest.TEST_JSON_FILTER)
78+
@FieldNameConstants
79+
@Getter
80+
class Bar {
81+
private String barField = "barValue";
82+
private String barDummy = "barDummy";
83+
}
84+
85+
```

‎_wiki/Java.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
layout : category
3+
title : Java
4+
summary :
5+
date : 2018-09-13 08:35:45 +0900
6+
updated : 2018-09-13 08:36:04 +0900
7+
tags : java
8+
toc : true
9+
public : true
10+
parent : index
11+
latex : false
12+
---
13+
* TOC
14+
{:toc}
15+
16+
#

‎_wiki/index.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout : wiki
33
title : index
44
summary : anyjava wiki index
55
date : 2018-08-15 19:41:36 +0900
6-
updated : 2018-09-06 02:01:37 +0900
6+
updated : 2018-09-13 08:37:20 +0900
77
tags :
88
toc : true
99
public : true
@@ -14,6 +14,8 @@ latex : false
1414
{:toc}
1515

1616
* [[개발환경세팅]]
17+
* [[Java]]
18+
* [[Jackson]]
1719
* [[Spring]]
1820
* [[Spring_Boot_Actuator]]
1921
* [[JPA]]

0 commit comments

Comments
 (0)