如何在 Jackson 中禁用 fail_on_empty_beans? [英] How do I disable fail_on_empty_beans in Jackson?

查看:27
本文介绍了如何在 Jackson 中禁用 fail_on_empty_beans?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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 中找到简单的教程或任何特别明显的东西.序列化工厂?为什么他们要让它如此不直观,然后让错误信息看起来如此简单?

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 with Jersey,我不记得我的头绪,但我相信它是相似的.

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).

这篇关于如何在 Jackson 中禁用 fail_on_empty_beans?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆