如何排除杰克逊不使用注释的字段?

我需要在渲染之前按名称排除某些字段。字段列表是动态的,因此我不能使用注释。

我尝试创建自定义序列化程序,但在那里找不到字段名称。

在GSON中,我使用了ExclusionStrategy,但是Jackson没有这种功能。有等同的吗?


慕标5832272
浏览 417回答 3
3回答

暮色呼如

下面的示例按名称排除字段来自我的博客文章Gson v Jackson-第4部分。(搜索了PropertyFilterMixIn。)这个例子演示了使用FilterProvider带有一个SimpleBeanPropertyFilter以serializeAllExcept字段名的用户指定的列表。@JsonFilter("filter properties by name")  class PropertyFilterMixIn {}  class Bar  {    public String id = "42";    public String name = "Fred";    public String color = "blue";    public Foo foo = new Foo();  }  class Foo  {    public String id = "99";    public String size = "big";    public String height = "tall";  }  public class JacksonFoo  {    public static void main(String[] args) throws Exception    {      ObjectMapper mapper = new ObjectMapper();      mapper.getSerializationConfig().addMixInAnnotations(          Object.class, PropertyFilterMixIn.class);      String[] ignorableFieldNames = { "id", "color" };      FilterProvider filters = new SimpleFilterProvider()        .addFilter("filter properties by name",             SimpleBeanPropertyFilter.serializeAllExcept(                ignorableFieldNames));      ObjectWriter writer = mapper.writer(filters);      System.out.println(writer.writeValueAsString(new Bar()));      // output:      // {"name":"James","foo":{"size":"big","height":"tall"}}    }  } (注意:在最新的Jackson版本中,相关的API可能已稍作更改。)尽管该示例确实使用了看似不必要的注释,但该注释并未应用于要排除的字段。(为帮助更改API,以简化必要的配置,请不要犹豫,为问题JACKSON-274的实现投票。

牧羊人nacy

我编写了一个库来处理类似的用例。我需要根据用户请求数据以编程方式忽略字段。普通的Jackson选项太笨拙了,我讨厌它使我的代码看起来很奇怪。该库使这一切更容易理解。它允许您简单地执行此操作:import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.module.SimpleModule;import com.monitorjbl.json.JsonView;import com.monitorjbl.json.JsonViewSerializer;import static com.monitorjbl.json.Match.match;//initialize jacksonObjectMapper mapper = new ObjectMapper();SimpleModule module = new SimpleModule();module.addSerializer(JsonView.class, new JsonViewSerializer());mapper.registerModule(module);&nbsp;//get a list of the objectsList<MyObject> list = myObjectService.list();String json;if(user.getRole().equals('ADMIN')){&nbsp; &nbsp; json = mapper.writeValueAsString(list);} else {&nbsp; &nbsp; json = mapper.writeValueAsString(JsonView.with(list)&nbsp; &nbsp; &nbsp; &nbsp; .onClass(MyObject.class, match()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.exclude("*")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.include("name")));}System.out.println(json);该代码可在GitHub上获得,希望对您有所帮助!

慕码人2483693

如果您在两个或多个pojo上定义了过滤器,则可以尝试以下操作:@JsonFilter("filterAClass")&nbsp;class AClass&nbsp;&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; public String id = "42";&nbsp;&nbsp;&nbsp; public String name = "Fred";&nbsp;&nbsp;&nbsp; public String color = "blue";&nbsp; public int sal = 56;&nbsp; public BClass bclass = new BClass();&nbsp;&nbsp;}&nbsp;&nbsp;//@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)@JsonFilter("filterBClass")&nbsp;class BClass&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp; public String id = "99";&nbsp;&nbsp;&nbsp; public String size = "90";&nbsp;&nbsp;&nbsp; public String height = "tall";&nbsp;&nbsp;&nbsp; public String nulcheck =null;&nbsp;&nbsp;}&nbsp;&nbsp;public class MultipleFilterConcept {&nbsp; &nbsp; public static void main(String[] args) throws Exception&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp;// Exclude Null Fields&nbsp; &nbsp; &nbsp; &nbsp; mapper.setSerializationInclusion(Inclusion.NON_NULL);&nbsp; &nbsp; &nbsp; &nbsp; String[] ignorableFieldNames = { "id", "color" };&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; String[] ignorableFieldNames1 = { "height","size" };&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; FilterProvider filters = new SimpleFilterProvider()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addFilter("filterAClass",SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addFilter("filterBClass", SimpleBeanPropertyFilter.serializeAllExcept(ignorableFieldNames1));&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ObjectWriter writer = mapper.writer(filters);&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(writer.writeValueAsString(new AClass()));&nbsp;&nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java