杰克逊:从json中删除一些值并保留一些空值 [英] Jackson: remove some values from json and keep some null values

查看:92
本文介绍了杰克逊:从json中删除一些值并保留一些空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的模型:

public class Employee {
    @JsonProperty("emplyee_id")
    private Integer id;
    @JsonProperty("emplyee_first_name")
    private String firstName;
    @JsonProperty("emplyee_last_name")
    private String lastName;
    @JsonProperty("emplyee_address")
    private String address;
    @JsonProperty("emplyee_age")
    private Byte age;
    @JsonProperty("emplyee_level")
    private Byte level;

    //getters and setters
}

现在我需要使用此(仅)模型创建两个JSON。

now I need to create two JSONs using this (only) model.

第一个必须这样,例如:

the first one must like this for example:

{
    "employee_id":101,
    "employee_first_name":"Alex",
    "employee_last_name":"Light",
    "employee_age":null,
    "employee_address":null
}

和第二个例如,一个人必须喜欢这样:

and the second one must like this for example:

{
    "employee_id":101,
    "employee_level":5
}

顺便说一下,我已经测试了 @JsonIgnore @JsonInclude(JsonInclude.Include.NON_NULL)

by the way, I already tested @JsonIgnore and @JsonInclude(JsonInclude.Include.NON_NULL).

第一个问题(据我所知),这些字段不能包含在其他JSON中(例如,如果 level 得到这个注释,它不会被包含在第二个JSON中)

the problem of the first one (as much as I know) is, those fields can't be included in other JSONs (for example if level get this annotation, it won't be included in the second JSON)

并且第二个问题是 null 值不能包含在JSON中。

and the problem of the second one is, null values can't be included in JSON.

所以我可以保留空值并防止其他一些属性包含在JSON没有创建额外的模型?如果答案是肯定的,我该怎么办呢?如果不是我真的很感激,如果有人给我这个州的最佳解决方案。

so can I keep null values and prevent some other property to be included in JSON without creating extra models? if the answer is yes, so how can I do it? if it's not I really appreciate if anyone gives me the best solution for this state.

非常感谢。

推荐答案

它对你使用@JsonView注释很有用

it could be useful for you using @JsonView annotation

public class Views {
    public static class Public {
    }
    public static class Base {
    }
 }



public class Employee {
   @JsonProperty("emplyee_id")
   @JsonView({View.Public.class,View.Base.class})
   private Integer id;

   @JsonProperty("emplyee_first_name")
   @JsonView(View.Public.class)
   private String firstName;

   @JsonProperty("emplyee_last_name")
   @JsonView(View.Public.class)
   private String lastName;

   @JsonProperty("emplyee_address")
   private String address;

   @JsonProperty("emplyee_age")
   private Byte age;

   @JsonProperty("emplyee_level")
   @JsonView(View.Base.class)
   private Byte level;

   //getters and setters
 }

在你的json中响应添加@JsonView(Public / Base.class)它将根据jsonview注释返回

in your json response add @JsonView(Public/Base.class) it will return based on jsonview annotations

//requestmapping
@JsonView(View.Public.class)  
public ResponseEntity<Employee> getEmployeeWithPublicView(){
    //do something
}

回复:

{ 
  "employee_id":101,
  "employee_first_name":"Alex",
  "employee_last_name":"Light",
  "employee_age":null,
  "employee_address":null
}

第二个

//requestmapping
@JsonView(View.Base.class)  
public ResponseEntity<Employee> getEmployeeWithBaseView(){
    //do something
}

响应

{
   "employee_id":101,
   "employee_level":5
}

这篇关于杰克逊:从json中删除一些值并保留一些空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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