jingyulog

JSON Filtering 본문

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;
}

'Tech > 스프링' 카테고리의 다른 글

@Primary, @Qualifier 둘 중 무엇을 써야하나?  (0) 2023.08.02
Resilience4j Circuit Breaker in Spring Boot3  (0) 2023.07.18
406 Not Acceptable 해결  (0) 2023.07.16
파일 업로드  (0) 2023.07.01
OAuth  (0) 2023.06.29