如何在Jackson中禁用fail_on_empty_beans? [英] How do I disable fail_on_empty_beans in Jackson?
问题描述
使用jackson 2.1,如何禁用错误消息似乎要禁用的 fail_on_empty beans
?
Using jackson 2.1, how do I disable the fail_on_empty beans
that the error message seems to want me to disable?
我认为这只是世界上最简单的事情,但是现在已经很晚了我无法找到一个简单的教程或任何特别明显的东西来自 api
。 SerializationFactory
?为什么他们会让它变得如此不直观,然后使错误信息看起来如此简单?
I'm assuming this is just the simplest thing in the world, but hell it is late and I haven't been able to find a simple tutorial or anything particularly obvious from the api
. SerializationFactory
? Why would they make it so unintuitive and then make the error message seem so straightforward?
虽然我做就像错误信息一样,我的意思是,它比NPE更好。
Although I do like the error message, I mean, it is better than an NPE.
我假设有一种方法可以使用注释来实现这一点 - 但我并不热衷于使用它们来简单化我正在做的工作!
I'm assuming there is a way to do this using annotations - but I'm not keen on using them at all for the simplistic work I'm doing!
推荐答案
我相信你可以在每个班级或全球范围内这样做。
You can do this per class or globally, I believe.
对于每个类,请在类声明上方尝试@JsonSerialize。
For per class, try @JsonSerialize above class declaration.
对于映射器,这是一个例子:
For a mapper, here's one example:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
// do various things, perhaps:
String someJsonString = mapper.writeValueAsString(someClassInstance);
SomeClass someClassInstance = mapper.readValue(someJsonString, SomeClass.class)
下面的StackOverflow链接还有一个Spring项目的例子。
The StackOverflow link below also has an example for a Spring project.
对于带泽西岛的REST,我不记得我的头顶,但我相信它是相似的。
For REST with Jersey, I don't remember off the top off my head, but I believe it's similar.
我挖出的几个链接:(由于Codehaus关闭而编辑的第一个链接)。
Couple of links I dug up: (edited 1st link due to Codehaus shutting down).
- https://web.archive.org/web/20150513164332/https://jira.codehaus.org/browse/JACKSON-201
- Jackson serializationConfig
- https://web.archive.org/web/20150513164332/https://jira.codehaus.org/browse/JACKSON-201
- Jackson serializationConfig
这篇关于如何在Jackson中禁用fail_on_empty_beans?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!