使用Jackson和Json-View排除json中的字段

时间:2017-05-18 12:10:03

标签: java json jackson json-view

我正在使用json-view根据我的需要创建一个动态json,它是一个很棒的库,我现在正在使用这个库一段时间。 最近我遇到了我的一个用例问题,让我先放置我的代码

用户类

public class User {

    private String name;
    private String emailId;
    private String mobileNo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }

    public String getMobileNo() {
        return mobileNo;
    }

    public void setMobileNo(String mobileNo) {
        this.mobileNo = mobileNo;
    }

}

ScreenInfoPojo类

public class ScreenInfoPojo {

    private Long id;
    private String name;
    private ScreenInfoPojo parentScreen;
    private User createdBy;
    private User lastUpdatedBy;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ScreenInfoPojo getParentScreen() {
        return parentScreen;
    }

    public void setParentScreen(ScreenInfoPojo parentScreen) {
        this.parentScreen = parentScreen;
    }



    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public User getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(User lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    } 

运行代码

public class TestMain {
    public static void main(String[] args) throws JsonProcessingException {
        User user=new User();
        user.setName("ABC");
        user.setEmailId("dev@abc123.com");
        user.setMobileNo("123456789");

        ScreenInfoPojo screen1=new ScreenInfoPojo();
        screen1.setId(1l);
        screen1.setName("Screen1");
        screen1.setCreatedBy(user);
        screen1.setLastUpdatedBy(user);

        ScreenInfoPojo screen2=new ScreenInfoPojo();
        screen2.setId(2l);
        screen2.setName("Screen2");
        screen2.setParentScreen(Screen1);
        screen2.setCreatedBy(user);
        screen2.setLastUpdatedBy(user);

        ScreenInfoPojo screen3=new ScreenInfoPojo();
        screen3.setId(3l);
        screen3.setName("Screen3");
        screen3.setParentScreen(Screen2);
        screen3.setCreatedBy(user);
        screen3.setLastUpdatedBy(user);

        ScreenInfoPojo screen4=new ScreenInfoPojo();
        screen4.setId(4l);
        screen4.setName("Screen4");
        screen4.setParentScreen(Screen3);
        screen4.setCreatedBy(user);
        screen4.setLastUpdatedBy(user);

        List<ScreenInfoPojo> screens=new ArrayList<>();
        screens.add(screen1);
        screens.add(screen2);
        screens.add(screen3);
        screens.add(screen4);

        ObjectMapper mapper = new ObjectMapper().registerModule(new JsonViewModule());
        String json = mapper.writeValueAsString(JsonView.with(screens).onClass(ScreenInfoPojo.class, Match.match()
                .exclude("*")
                .include("id","name","createdBy.name","lastUpdatedBy.mobileNo","parentScreen.id")));
        System.out.println("json"+json);
    }    

结果

[{
    "id": 1,
    "name": "Screen1",
    "parentScreen": null,
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 2,
    "name": "Screen2",
    "parentScreen": {
        "id": 1,
        "name": "Screen1",
        "parentScreen": null,
        "createdBy": {},
        "lastUpdatedBy": {}
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 3,
    "name": "Screen3",
    "parentScreen": {
        "id": 2,
        "name": "Screen2",
        "parentScreen": {
            "id": 1,
            "name": "Screen1",
            "parentScreen": null,
            "createdBy": {},
            "lastUpdatedBy": {}
        },
        "createdBy": {},
        "lastUpdatedBy": {}
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 4,
    "name": "Screen4",
    "parentScreen": {
        "id": 3,
        "name": "Screen3",
        "parentScreen": {
            "id": 2,
            "name": "Screen2",
            "parentScreen": {
                "id": 1,
                "name": "Screen1",
                "parentScreen": null,
                "createdBy": {},
                "lastUpdatedBy": {}
            },
            "createdBy": {},
            "lastUpdatedBy": {}
        },
        "createdBy": {},
        "lastUpdatedBy": {}
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}]

预期结果

[{
    "id": 1,
    "name": "Screen1",
    "parentScreen": null,
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 2,
    "name": "Screen2",
    "parentScreen": {
        "id": 1
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 3,
    "name": "Screen3",
    "parentScreen": {
        "id": 2
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}, {
    "id": 4,
    "name": "Screen4",
    "parentScreen": {
        "id": 3
    },
    "createdBy": {
        "name": "ABC"
    },
    "lastUpdatedBy": {
        "mobileNo": "123456789"
    }
}]

问题

在我的用例中,我有一个类 ScreenInfoPojo ,它引用与 parentScreen 相同的类, 我正在尝试获取父项的特定字段/字段(“parentScreen.id”)instate我获取了我在子/目标对象上定义的所有字段(“id”,“name “,”createdBy.name“,”lastUpdatedBy.mobileNo“,”parentScreen.id“)并且父响应再次递归!我观察到的一件事情只有在课程有自己的参考时才会发生,我将用户类引用放在两个不同的字段 createdBy lastUpdatedBy 并试图获取“name”“mobileNo”分别工作得很好。

任何解决此问题的建议都会非常有用!!!!

由于

3 个答案:

答案 0 :(得分:3)

您可以在json响应中不需要的字段上使用jackson注释 @jsonignore

我不知道您是否可以在代码上使用任何注释。如果是这样就没用了..

答案 1 :(得分:2)

序列化对象最灵活的方法是编写自定义序列化程序。

如果我正确理解了您的要求,则以下序列化程序可能有效:

public class CustomScreenInfoSerializer extends JsonSerializer<ScreenInfoPojo> {

    @Override
    public void serialize(ScreenInfoPojo value, JsonGenerator gen, SerializerProvider serializers)
            throws IOException, JsonProcessingException {
        gen.writeStartObject();
        gen.writeNumberField("id", value.getId());
        gen.writeStringField("name", value.getName());
        gen.writeFieldName("createdBy");
        gen.writeStartObject();
        gen.writeStringField("name", value.getCreatedBy().getName());
        gen.writeEndObject();
        gen.writeFieldName("lastUpdatedBy");
        gen.writeStartObject();
        gen.writeStringField("mobileNo", value.getLastUpdatedBy().getMobileNo());
        gen.writeEndObject();
        if (value.getParentScreen() == null) {
            gen.writeNullField("parentScreen");
        }
        else {
            gen.writeFieldName("parentScreen");
            gen.writeStartObject();
            gen.writeNumberField("id", value.getParentScreen().getId());
            gen.writeEndObject();
        }
        gen.writeEndObject();

    }

}

使用

ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(ScreenInfoPojo.class, new CustomScreenInfoSerializer());
mapper.registerModule(module);
String json = mapper.writeValueAsString(screens);
System.out.println(json);

产生

[
   {
      "id": 1,
      "name": "Screen1",
      "createdBy": {
         "name": "ABC"
      },
      "lastUpdatedBy": {
         "mobileNo": "123456789"
      },
      "parentScreen": null
   },
   {
      "id": 2,
      "name": "Screen2",
      "createdBy": {
         "name": "ABC"
      },
      "lastUpdatedBy": {
         "mobileNo": "123456789"
      },
      "parentScreen": {
         "id": 1
      }
   },
   {
      "id": 3,
      "name": "Screen3",
      "createdBy": {
         "name": "ABC"
      },
      "lastUpdatedBy": {
         "mobileNo": "123456789"
      },
      "parentScreen": {
         "id": 2
      }
   },
   {
      "id": 4,
      "name": "Screen4",
      "createdBy": {
         "name": "ABC"
      },
      "lastUpdatedBy": {
         "mobileNo": "123456789"
      },
      "parentScreen": {
         "id": 3
      }
   }
]

答案 2 :(得分:2)

是。 Include子句不适用于引用同一个类。

你能做到吗?

根据github指令build from source

从源代码编译

更新函数JsonViewSerializer.JsonWriter.fieldAllowed

查找

        if(match == null) {
            match = this.currentMatch;
        } else {
            prefix = "";
        }

和评论其他条款

        if(match == null) {
            match = this.currentMatch;
        } else {
            //prefix = "";
        }

您将获得预期的结果。但。我不知道它会如何影响另一个过滤器。

要获得更多控制权,可以向JsonView类添加属性。

例如:

在JsonView中添加:

private boolean ignorePathIfClassRegistered = true;

     public boolean isIgnorePathIfClassRegistered() {
            return ignorePathIfClassRegistered;
        }

        public JsonView1<T>  setIgnorePathIfClassRegistered(boolean ignorePathIfClassRegistered) {
            this.ignorePathIfClassRegistered = ignorePathIfClassRegistered;
            return this;
        }

在JsonViewSerializer.JsonWriter.fieldAllowed函数中重写if子句:

        if(match == null) {
            match = this.currentMatch;
        } else {
            if (result.isIgnorePathIfClassRegistered())
            prefix = "";
        }

你可以在你的例子中使用它,如:

JsonView<List<ScreenInfoPojo>> viwevedObject = JsonView
        .with(screens)
        .onClass(ScreenInfoPojo.class,
                Match.match()
                        .exclude("*")
                        .include("id","name")
                        .include("createdBy.name")
                        .include("lastUpdatedBy.mobileNo")
                        .include("parentScreen.id"))
        .setIgnorePathIfClassRegistered(false);

ObjectMapper mapper = new ObjectMapper().registerModule(new JsonViewModule());
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);

String json = mapper.writeValueAsString(viwevedObject);