📅  最后修改于: 2020-11-16 07:42:41             🧑  作者: Mango
@JsonFilter用于在序列化/反序列化期间应用过滤器,例如要使用或不使用哪些属性。
import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
public class JacksonTester {
public static void main(String args[]) throws IOException, ParseException {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student(1,13, "Mark");
FilterProvider filters = new SimpleFilterProvider() .addFilter(
"nameFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"));
String jsonString = mapper.writer(filters)
.withDefaultPrettyPrinter()
.writeValueAsString(student);
System.out.println(jsonString);
}
}
@JsonFilter("nameFilter")
class Student {
public int id;
public int rollNo;
public String name;
Student(int id, int rollNo, String name) {
this.id = id;
this.rollNo = rollNo;
this.name = name;
}
}
{
"name" : "Mark"
}