목차

Jackson JsonFilter

Nested Filter

Filter nested objects using Jackson's BeanPropertyFilter - Stack Overflow

@JsonFilter("personFilter")
public class Person {
    private Name name;
    private int age;
    public Name getName() {return name;}
    public void setName(Name name) {this.name = name;}
    public int getAge() {return age;}
    public void setAge(int age) {this.age = age;}
}
 
@JsonFilter("nameFilter")
public class Name {
    private String firstName;
    private String lastName;
    public String getFirstName() {return firstName;}
    public void setFirstName(String firstName) {this.firstName = firstName;}
    public String getLastName() {return lastName;}
    public void setLastName(String lastName) {this.lastName = lastName;}
}
And then add 2 filters, one for Person and one for Name:
 
FilterProvider filterProvider = new SimpleFilterProvider()
            .addFilter("personFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"))
            .addFilter("nameFilter", SimpleBeanPropertyFilter.filterOutAllExcept("firstName"));

Spring 4.2 JsonFilter 지원

  @RequestMapping(method=RequestMethod.GET)
  public MappingJacksonValue getMovies(@AuthenticatedUser TutorialUser user){
    List<Movie> movieList = movieDao.findAll();
    MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(movieList);
 
    mappingJacksonValue.setFilters(new SimpleFilterProvider().addFilter("movie", 
      SimpleBeanPropertyFilter.filterOutAllExcept(user!=null && user.isAdmin() ? all : limited)));
 
    return mappingJacksonValue;
  }

참고