Jackson ObjectMapper - 从远程响应反序列化后,如何在值为 false 时排除字段?

凯夫 D。

我正在对外部服务 A 进行远程调用,它会将响应作为 json 返回,然后使用对象映射器反序列化为MyResponse对象。之后,在我当前的服务中,我需要将此对象和输出附加到 UI。

MyResponse服务 A中的字段之一是布尔值,我只希望我的 UI 响应在值为 true 时包含此字段。请注意,我无权修改我的MyResponse对象,因为它是只读的。所以我创建了一个MixIn类,也尝试了几种方法,但没有用。

    public class MyResponse {

        private String stringValue;
        private int intValue;
        // Expectation: only include this field when value true, and exclude it when value is false
        private boolean booleanValue;
    }

    // @JsonIgnoreProperties(value = { "booleanValue" })
    // @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY)
    private static class MixInMyResponse {

    }

    // this would be my rest service eventually send myResponse to UI
    public MyResponse readFromRemote() throws IOException {
        String jsonAsString =
            "{\"stringValue\":\"a\",\"intValue\":1,\"booleanValue\":false}";
        ObjectMapper mapper = new ObjectMapper();
        // configure object mapper with mix in
        mapper.getDeserializationConfig().addMixInAnnotations(MyResponse.class, MixInMyResponse.class);
        MyResponse myResponse = mapper.readValue(jsonAsString, MyResponse.class);
        // Expectation: writeValue needs only include booleanValue when value true, and exclude booleanValue when value is false
        String writeValue = mapper.writeValueAsString(myResponse);
        System.out.println(writeValue);
        return myResponse;
    }

  1. 使用@JsonIgnoreProperties(value = { "booleanValue" }):当 value 为 false 时,这可以解决问题,但当 value 为 true 时,它​​也不包含该字段
  2. 使用 @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY): 当值为 时false,这会将字段反序列booleanValue化为 false,因此我返回的myResponse/writeValue字段仍将具有 UI。

对此有任何其他建议吗?

埃塞克斯男孩

编写自己的序列化程序

public class MyResponseSerializer extends JsonSerializer<MyResponse> {
    @Override
    public void serialize(MyResponse myResponse, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("stringValue", myResponse.getStringValue());
        jsonGenerator.writeNumberField("intValue", myResponse.getIntValue());
        if (myResponse.isBooleanValue()) {
            jsonGenerator.writeBooleanField("booleanValue", myResponse.isBooleanValue());
        }
        jsonGenerator.writeEndObject();
    }
}

使用 Objectmapper 注册它

@Test
public void test3() throws IOException {
    MyResponse response1 = new MyResponse("response1", 1, true);
    MyResponse response2 = new MyResponse("response2", 1, false);

    ObjectMapper objectMapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addSerializer(MyResponse.class, new MyResponseSerializer());
    objectMapper.registerModule(module);
    System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(response1));
    System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(response2));
}

看这个教程

当我看到您无法修改 MyResponse 以添加注释时,我编辑了原始答案。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Jackson的objectMapper反序列化接口字段?

如何配置Jackson ObjectMapper将Foo类型的所有字段反序列化为SubclassOfFoo的实例?

使用多个useWrapping = false时,Jackson XML反序列化将跳过字段

如何使用Jackson ObjectMapper序列化从JSON响应中替换空字段(嵌套在所有级别)?

Jackson ObjectMapper:如何从序列化中忽略(忽略)某些类型的字段?

Jackson ObjectMapper用数组反序列化对象

为什么Jackson Jackson ObjectMapper给我这个将JSON字段转换为Date对象的错误?无法从字符串反序列化类型为java.util.Date的值

当Object变量设置为Private时,Jackson ObjectMapper返回null

在使用Jackson ObjectMapper时如何忽略pojo注释?

解析为Object时,Jackson ObjectMapper readValue()无法识别的字段

如何使用ObjectMapper Jackson反序列化泛型类型

如何使用Jackson ObjectMapper反序列化Spring的ResponseEntity(可能使用@JsonCreator和Jackson mixins)

Jackson ObjectMapper的日期和时间戳序列化

Jackson ObjectMapper-指定对象属性的序列化顺序

即使明显禁用了反序列化,Jackson ObjectMapper也会将日期调整为上下文时区

使用JACKSON ObjectMapper后从POJO获取null

如何指示Jackson ObjectMapper不要将数字字段值转换为String属性?

反序列化具有关键变量 jackson map ObjectMapper 的 JSON

使用 Jackson ObjectMapper 双向一对多反序列化 JSON 对象

如何正确地重用Jackson ObjectMapper?

如何用Guice / Jersey钩住Jackson ObjectMapper

如何创建确定性的Jackson ObjectMapper?

当值为“ null”时,Jackson忽略自定义字段反序列化器

禁用每个ObjectMapper的字段序列化

objectmapper 僅序列化特定字段

反序列化时如何防止Jackson将kotlin.Boolean参数设置为false

当我使用Jackson ObjectMapper()类的writeValueAsString()方法时,如何解决此无限递归(StackoverflowError)?

如何在Spring Boot中为Camel配置Jackson ObjectMapper

我应该将Jackson的ObjectMapper声明为静态字段吗?