我正在使用@JsonFilter忽略Json响应中发送的某些属性。 当我通过邮递员在我的应用程序启动并运行时运行控制器时,这工作得很好。但是,当我尝试使用嘲讽来测试json响应时,应该被忽略的属性仍然存在。
@Data
@JsonFilter("empFilter")
public class Employee {
private int id;
private String name;
private int age;
private Integer test;
}
@GetMapping("/employee/ignoreProperties")
public MappingJacksonValue retrieveSomeBean() {
Employee emp = new Employee();
emp.setAge(10);
emp.setId(1);
emp.setName("ana");
emp.setTest(9);
Set<String> set = new HashSet<>();
set.add("test");
return filterJson(emp, "empFilter", set);
}
private MappingJacksonValue filterJson(Object object, String filterName, Set<String> properties) {
SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.serializeAllExcept(properties);
FilterProvider filters = new SimpleFilterProvider()
.setFailOnUnknownId(false)
.addFilter(filterName, filter);
MappingJacksonValue mapping = new MappingJacksonValue(object);
mapping.setFilters(filters);
return mapping;
}