Tech/스프링
JSON Filtering
jingyulog
2023. 7. 16. 16:50
방법 1. @JsonIgnore
필터를 적용하고자 하는 필드위에 적용시킬 수 있다.
@JsonIgnore
private String field2;
방법2. @JsonIgnoreProperties
필터를 적용하고자 하는 클래스위에 적용시킬 수 있다.
@JsonIgnoreProperties({"field1","field3"})
public class SomeBean {
private String field1;
@JsonIgnore
private String field2;
private String field3;
}
방법3. MappingJacksonValue
필터를 적용하고자 하는 컨트롤러 코드안에서 dynamic하게 적용시킬 수 있다.
@GetMapping(path = "/filtering")
public MappingJacksonValue getSomeBean() {
// MappingJacksonValue
SomeBean someBean = new SomeBean("value1", "value2", "value3");
MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(someBean);
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept("field1", "field3");
FilterProvider filters = new SimpleFilterProvider().addFilter("SomeBeanFilter", filter);
mappingJacksonValue.setFilters(filters);
return mappingJacksonValue;
}
@JsonFilter(value = "SomeBeanFilter")
public class SomeBean {
private String field1;
// @JsonIgnore
private String field2;
private String field3;
}