简体   繁体   English

使用Jackson的自定义JSON字段作为响应

[英]Custom JSON fields with Jackson in response

I have some RESTful WS created with Spring Boot and one of some endpoint's methods returns instance of some class which will be converted then into JSON using embedded Jackson library. 我使用Spring Boot创建了一些RESTful WS,某些端点的方法之一返回了某个类的实例,该类将使用嵌入式Jackson库转换为JSON。 Jackson converts to JSON every field, even if some fields are null. 即使某些字段为空,Jackson也会将每个字段转换为JSON。 So in the output it will look like: 因此,在输出中它将如下所示:

{
    "field1": "res1",
    "field2": "res2",
    "field3": null
}

I want to ignore some field it the output in particular cases. 我想在某些情况下忽略某些字段的输出。 Not everytime, at some cases. 在某些情况下,并非每次都这样。 How to do it? 怎么做?

To suppress serializing properties with null values using Jackson >2.0, you can configure the ObjectMapper directly, or make use of the @JsonInclude annotation: 要使用Jackson> 2.0抑制具有空值的序列化属性,可以直接配置ObjectMapper,或使用@JsonInclude批注:

mapper.setSerializationInclusion(Include.NON_NULL);

or: 要么:

@JsonInclude(Include.NON_NULL)
class Foo
{
  String field1;
  String field2;
  String field3;
}

Alternatively, you could use @JsonInclude in a getter so that the attribute would be shown if the value is not null. 或者,您可以在getter中使用@JsonInclude,以便在值不为null时显示属性。

Complete example available at How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson 如何防止Map中的空值和Bean中的空字段通过Jackson序列化的完整示例

To exclude null values you can use 要排除空值,您可以使用

@JsonInclude(value = Include.NON_NULL)
public class YourClass {
}

And to include customised values you can use 并包含您可以使用的自定义值

public class Employee {
  private String name;
  @JsonInclude(value = JsonInclude.Include.CUSTOM, valueFilter = DateOfBirthFilter.class)
  private Date dateOfBirth;
  @JsonInclude(content = JsonInclude.Include.CUSTOM, contentFilter = PhoneFilter.class)
  private Map<String, String> phones;
}

public class DateOfBirthFilter {

  @Override
  public boolean equals(Object obj) {
      if (obj == null || !(obj instanceof Date)) {
          return false;
      }
      //date should be in the past
      Date date = (Date) obj;
      return !date.before(new Date());
  }
}

public class PhoneFilter {
  private static Pattern phonePattern = Pattern.compile("\\d{3}-\\d{3}-\\d{4}");

  @Override
  public boolean equals(Object obj) {
      if (obj == null || !(obj instanceof String)) {
          return false;
      }
      //phone must match the regex pattern
      return !phonePattern.matcher(obj.toString()).matches();
  }
}

Took reference from https://www.logicbig.com/tutorials/misc/jackson/json-include-customized.html https://www.logicbig.com/tutorials/misc/jackson/json-include-customized.html获取了参考

At the top your class add the NON_NULL Jackson annotation to ignore null values when receiving or sending 在您的类的顶部,添加NON_NULL Jackson批注以在接收或发送时忽略空值

@JsonInclude(value = Include.NON_NULL)
public class SomeClass {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM