ObjectMapper setSerializationInclusion(JsonInclude.Include.NON_NULL) 设置未反映出来
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,当序列化对象、自定义对象或
Map
时,此选项适用,但在使用 json 树时不适用于。考虑这个Foo
类:排除空值的选项将按预期工作。
主要方法来说明:
This option is applicable when serializing objects, custom objects or a
Map
for example, but not when working with json tree. Consider thisFoo
class:The option to exclude nulls will work as expected with it.
Main method to illustrate it: