반응형
JSON 형식으로 서버에 로그를 남기고 있습니다.
그런데 객체를 통으로 JSON으로 변환하다 보니 원하지 않거나 혹은 데이터를 변형해서 남기고 싶을 때가 있습니다.
여러가지 방법이 있지만 자유도 측면에서 custom filter 를 애용합니다.
아래 예제코드를 참조하시면 입맛에 맛게 마음대로 JSON 변환을 제어할 수 있습니다.
예제 코드
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.springframework.util.ReflectionUtils;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
public class LogJsonFilter extends SimpleBeanPropertyFilter {
static final List<String> maskingTtargetNames = Arrays.asList("number");
static final List<String> exceptTtargetNames = Arrays.asList("price");
public void serializeAsField(Object target, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer)
throws Exception {
String name = writer.getName();
boolean isTarget = false;
if (maskingTtargetNames.contains(name)) { // 마스킹처리
if (target instanceof String) {
super.serializeAsField(FormatUtil.getSecurity(String.valueOf(target)), jgen, provider, writer);
isTarget = true;
} else {
Field field = ReflectionUtils.findField(target.getClass(), name);
if (field != null) {
boolean orgAccessible = field.isAccessible();
field.setAccessible(true);
Object value = ReflectionUtils.getField(field, target);
if (value instanceof String) {
jgen.writeFieldName(name);
jgen.writeString(FormatUtil.getSecurity(String.valueOf(value)));
isTarget = true;
}
field.setAccessible(orgAccessible);
}
}
} else if (exceptTtargetNames.contains(name)) { // json에서 제외처리
isTarget = true;
}
if (!isTarget) {
// 변경없음
super.serializeAsField(target, jgen, provider, writer);
}
}
public static FilterProvider getFilter() {
SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider();
FilterProvider filters = simpleFilterProvider.setDefaultFilter(new LogJsonFilter());
return filters;
}
"classFilter") (
public class ClassMixIn {
}
public static void main(String[] args) throws JsonProcessingException {
Car car = new Car();
car.setNumber("18다0210");
car.setColor("Red");
car.setPrice("1억9천만원");
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Object.class, ClassMixIn.class);
System.out.println("--필터적용--");
System.out.println(mapper.writer(LogJsonFilter.getFilter()).writeValueAsString(car));
System.out.println("");
mapper = new ObjectMapper();
System.out.println("--필터없이--");
System.out.println(mapper.writer().writeValueAsString(car));
}
static class Car {
private String number;
private String color;
private String price;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
}
* FormatUtil.getSecurity 는 Masking 처리해주는 함수 입니다.
결과
--필터적용--
{"number":"18***10","color":"Red"}
--필터없이--
{"number":"18다0210","color":"Red","price":"1억9천만원"}
반응형
'java' 카테고리의 다른 글
maven cache clear. 메이븐 캐시 클리어. (0) | 2018.07.31 |
---|---|
spring boot 2.0 default connection pool (2) | 2018.07.25 |
fasterxml json parsing uppsercase (0) | 2018.07.24 |
spring resttemplate httpclient connection pool (0) | 2018.07.24 |
spring boot multi db and jndi in mybatis (0) | 2018.07.13 |
JNDI TEST (0) | 2018.07.09 |
fasterxml json camelcase (2) | 2018.06.18 |
spring jdbc sample (0) | 2018.06.04 |