ObjectMapper setSerializationInclusion(JsonInclude.Include.NON_NULL) 设置未反映出来

发布于 2025-01-16 11:37:14 字数 1373 浏览 1 评论 0原文

ObjectMapper 仍然包含空值。我尝试了这里找到的很多解决方案,但没有任何效果。我无法使用json注释,所以我唯一的解决方案是映射器的预定义设置,但这没有反映出来。我认为这是由于 objectMapper 的缓存造成的。但我对映射器的唯一修改是在构造函数中进行的。所以缓存不会成为问题

依赖关系:

Log4J2: 2.17.1
Fasterxml Jackson annotation: 2.13.2
Fasterxml Jackson databind: 2.13.2
Wildfly: 20.0.1
OpenJDK: 11.0.14.1

我有一个 objectMapper 定义为全局值,它在构造函数中实例化。然后我有一种方法来构建接受键和值的 JSON。因为价值可以是任何东西。

private final ObjectMapper jsonMapper;

public SomeConstructor() {
    this.jsonMapper = new ObjectMapper();
    this.jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    this.jsonMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
}

@Override
public void setJsonVar(String jsonVar, String jsonKey, Object values) {

    // loads ObjectNode from memory if exists
    ObjectNode jsonNode = getJsonVar(jsonVar);

    // lazy init if ObjectNode not exists
    if (jsonNode == null) {
        jsonNode = jsonMapper.createObjectNode();
    }
    // add object
    jsonNode.putPOJO(jsonKey, values);
}

用法:

setJsonVar("var-A", "key-A", 1);
setJsonVar("var-A", "key-B", null);
print("var-a");

期望:

我想避免 JSON 中的空值。

预期:var-A: { "key-A":1 } 得到: var-A: { "key-A":1, "key-B":null }

为什么会发生这种情况,我可以采取什么措施来解决这个问题?

ObjectMapper still includes null values. I tried a lot of solutions found here, but nothing works. I cannot use json annotation, so my only solution is predefined setting of mapper, but this was not reflected. I thought this was due to caching of objectMapper. But my only modifies of mapper are made in Constructor. So caching would not be a problem

Dependencies:

Log4J2: 2.17.1
Fasterxml Jackson annotation: 2.13.2
Fasterxml Jackson databind: 2.13.2
Wildfly: 20.0.1
OpenJDK: 11.0.14.1

I have an objectMapper defined as global value which is instantiated in constructor. Then I have one method for building a JSON which accepts key and value. As value can by anything.

private final ObjectMapper jsonMapper;

public SomeConstructor() {
    this.jsonMapper = new ObjectMapper();
    this.jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    this.jsonMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
}

@Override
public void setJsonVar(String jsonVar, String jsonKey, Object values) {

    // loads ObjectNode from memory if exists
    ObjectNode jsonNode = getJsonVar(jsonVar);

    // lazy init if ObjectNode not exists
    if (jsonNode == null) {
        jsonNode = jsonMapper.createObjectNode();
    }
    // add object
    jsonNode.putPOJO(jsonKey, values);
}

Usage:

setJsonVar("var-A", "key-A", 1);
setJsonVar("var-A", "key-B", null);
print("var-a");

Expectation:

I want to avoid null values in JSON.

Expected: var-A: { "key-A":1 }
Got: var-A: { "key-A":1, "key-B":null }

Why does this happen and what can I do to work around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2025-01-23 11:37:14

例如,当序列化对象、自定义对象或 Map 时,此选项适用,但在使用 json 树时不适用于。考虑这个 Foo 类:

public class Foo {

    private String id;
    private String name;

    //getters and setters
}

排除空值的选项将按预期工作。

主要方法来说明:

public class Main {

    public static void main(String[] args) throws Exception {
        serializeNulls();
        System.out.println();
        doNotSerializeNulls();
    }

    private static void serializeNulls() throws Exception {
        ObjectMapper jsonMapper = new ObjectMapper();
        Foo foo = new Foo();
        foo.setId("id");
        System.out.println("Serialize nulls");
        System.out.println(jsonMapper.writeValueAsString(foo));

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("key1", "val1");
        map.put("key2", null);
        System.out.println(jsonMapper.writeValueAsString(map));
    }

    private static void doNotSerializeNulls() throws Exception {
        ObjectMapper jsonMapper = new ObjectMapper();
        jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        jsonMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);

        Foo foo = new Foo();
        foo.setId("id");
        System.out.println("Do not serialize nulls");
        System.out.println(jsonMapper.writeValueAsString(foo));

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("key1", "val1");
        map.put("key2", null);
        System.out.println(jsonMapper.writeValueAsString(map));
    }
}

This option is applicable when serializing objects, custom objects or a Map for example, but not when working with json tree. Consider this Foo class:

public class Foo {

    private String id;
    private String name;

    //getters and setters
}

The option to exclude nulls will work as expected with it.

Main method to illustrate it:

public class Main {

    public static void main(String[] args) throws Exception {
        serializeNulls();
        System.out.println();
        doNotSerializeNulls();
    }

    private static void serializeNulls() throws Exception {
        ObjectMapper jsonMapper = new ObjectMapper();
        Foo foo = new Foo();
        foo.setId("id");
        System.out.println("Serialize nulls");
        System.out.println(jsonMapper.writeValueAsString(foo));

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("key1", "val1");
        map.put("key2", null);
        System.out.println(jsonMapper.writeValueAsString(map));
    }

    private static void doNotSerializeNulls() throws Exception {
        ObjectMapper jsonMapper = new ObjectMapper();
        jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        jsonMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);

        Foo foo = new Foo();
        foo.setId("id");
        System.out.println("Do not serialize nulls");
        System.out.println(jsonMapper.writeValueAsString(foo));

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("key1", "val1");
        map.put("key2", null);
        System.out.println(jsonMapper.writeValueAsString(map));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文