Jackson2 PropertyFilter用于嵌套属性,还是有另一种方法? [英] Jackson2 PropertyFilter for nested properties, or is there another way?

查看:104
本文介绍了Jackson2 PropertyFilter用于嵌套属性,还是有另一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找一种以多种方式序列化对象而不直接干扰类的方法。 (比如添加大量注释)

I'm currently searching a way to serialize an object in multiple ways without interfering with the classes directly. (like adding lots of annotations)

想象一下这样的一个类:

Imagine a class like this:

class User {
  String id;
  String name;
  String email;
  ...
  User manager;
}

SimplePropertyFilter 提供几乎所需的功能,但问题是这个过滤器忽略当前对象的级别,并在每个对象上应用所有已定义的过滤器,无论它是否是嵌套的。

The SimplePropertyFilter delivers almost the desired functionality but the problem is that this filter ignores the level of the current object and applies all defined filters on each object, whether it is a nested one or not.

我不仅限于过滤器,但我想避免为每个所需的json响应编写多个类。有没有办法用Jackson2实现这种行为?

I'm not limited to Filters, but I would like to avoid writing multiple classes for each of the desired json response. Is there a way to achieve this behavior with Jackson2 ?

如果我们想为编辑表单序列化这个,我们需要所有字段,但对于用户列表,我们可能只需要其中一些。

If we would like to serialize this for an edit form we need all fields, but for a list of users we probably just need some of them.

对于经理而言我们总是需要只 id 名称。这就是搜索解决方案。

For the manager however we always would need "only" the id and the name. And this is what search a solution for.

JSON输出应该是这样的:

The JSON output should become something like this:

{
  "id" : "20",
  "login" : "USER20",
  "name" : "User 20",
  "email" : "user20@no.where",
  "manager" : {
     "id" : "1",
     "name" : "Administrator"
  }
}






我觉得属性过滤方法让我非常接近理想的解决方案,但从我发现的只能定义简单属性。


I feel like the property filter approach is getting me quite close to the desired solution but from what i found out you can only define "simple" properties.

public class UserJsonWriter {
  public String toJson() {
    SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept("id", "login", "name", "email", "manager");
    // Unfortunatly I cannot write:
    SimpleBeanPropertyFilter filter = SimpleBeanPropertyFilter.filterOutAllExcept("id", "login", "name", "email", "manager.id", "manager.name");


    User user = userService.getUser(20);
    ObjectMapper copy = objectMapperFactoryBean.getCopy();
    copy.addMixInAnnotations(User.class, CustomFilter.class);
    ObjectWriter writer = copy.writer(new SimpleFilterProvider().addFilter("customFilter", filter));
    return writer.writeValueAsString(user);
  }
}

@JsonFilter("customFilter")
public class CustomFilter {

}

目前输出看起来像这样。

And currently the output looks like this.

{
  "id" : "20",
  "login" : "USER20",
  "name" : "User 20",
  "email" : "user20@no.where",
  "manager" : {
     "id" : "1",
     "login" : "ADMIN",
     "name" : "Administrator",
     "email" : "admin@no.where",
     "manager" : {
       ... (might continue multiple times)
     }
   }
}






据我所知,PropertyFilter不知道序列化的当前位置,json生成器(直接)也不知道。


From what I can tell, the PropertyFilter does not know the current "location" of the serialization, neither does the json generator (directly).

我可以看到使用的最简单的解决方案在此过滤过程中查找嵌套属性。这种可能性存在于杰克逊吗?可能有其他库支持这种行为吗?

The simplest solution that i can see to be used would look for nested properties during this filtering process. Does this possibility exist in Jackson? Probably there are other libraries that support this behaviour?

编辑:我还想补充说我正在使用Spring MVC,可能有一种方法可以通过Spring?

I also like to add that I'm using Spring MVC, probably there is a way through Spring?

谢谢

推荐答案

用YES和一个小插件库回答我自己的问题在github上。

Answering my own question with YES and a little addon library on github.

https://github.com / Antibrumm / jackson-antpathfilter

这篇关于Jackson2 PropertyFilter用于嵌套属性,还是有另一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆