泽西岛的 JSON ArrayList

发布于 2024-11-26 19:41:41 字数 240 浏览 2 评论 0原文

我试图从 jersey 返回一个列表,它在 XML 中工作正常,但是当我将其输出为 JSON 时,它声称“Java 类......和 ​​Java 类型......以及 MIME 媒体的消息正文编写器”找不到类型 application/json”。

我没有做任何处理实用程序列表的配置,因为我认为 jersey + jersey-json-1.9 自动为 JSON 处理这些东西,就像处理 XML 一样。

还有其他人有这样的运气吗?

I'm trying to return a List from jersey, which works fine in XML, but when I go to output it as JSON, it claims, "A message body writer for Java class ... and Java type ... and MIME media type application/json was not found".

I have not done any configuration for dealing with utility lists, as i thought that jersey + jersey-json-1.9 handled this stuff automagically for JSON the same way it has for XML.

Anyone else have any luck with this?

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

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

发布评论

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

评论(4

当梦初醒 2024-12-03 19:41:41

我发现我对同一问题的第一次尝试失败并出现此错误消息,解决方案如另一个 SO 问题中给出的,Jersey:com.sun.jersey.server.impl.template.ViewableMessageBodyWriter:我忘记添加jersey-json 模块添加到我的项目中。

I found that my first stab at this same problem failed with this error message, and the solution was as given in another SO question, Jersey: com.sun.jersey.server.impl.template.ViewableMessageBodyWriter: I had forgotten to add the jersey-json module to my project.

何以笙箫默 2024-12-03 19:41:41

您不需要任何带有 JSON 的列表包装器,但您确实需要启用“POJO 映射”样式的 JSON 支持。

You should not need any wrappers for lists with JSON, but you do need to enable "POJO mapping" style of JSON support.

站稳脚跟 2024-12-03 19:41:41

回答了。这需要创建一个提供者。代码如下:

@Provider
@Singleton
@Produces(MediaType.APPLICATION_JSON)
public class ContextResolver extends JacksonJaxbJsonProvider{

     public ContextResolver() throws Exception {
        super();
        ObjectMapper mapper = new ObjectMapper();
        setMapper(mapper);
    }
}

Answered. This required the creation of a provider. Code given below:

@Provider
@Singleton
@Produces(MediaType.APPLICATION_JSON)
public class ContextResolver extends JacksonJaxbJsonProvider{

     public ContextResolver() throws Exception {
        super();
        ObjectMapper mapper = new ObjectMapper();
        setMapper(mapper);
    }
}
无人接听 2024-12-03 19:41:41

我设法解决了最近 Jersey json 库中的 JSON 数组“bug”(v1.14 Sep 2012)。秘密成分是 JSONConfiguration 和 ContextResolver 魔法。请参阅我的下一篇文章,它有一个完整的代码示例,自定义的 ContextResolver 和其余的 Application 类乍一看可能有点模糊逻辑。

如何使用 Jersey REST 序列化 Java 原语

基元和零或单元素列表数组已正确序列化为 JSON 文档。如果没有自定义解析器,如果 Java 列表为空,您将无法获得正确的 json[] 数组字段。我的帖子列出了您需要的所有 .jar 库。我正在使用最新的 v1.14 Jersey 存档。

I managed to solve JSON array "bug" in recent Jersey json library (v1.14 Sep 2012). Secret ingredient is JSONConfiguration and ContextResolver magic. See my following post it has a full code example, customized ContextResolver and rest Application class might be somewhat fuzzy logic in first look.

How to serialize Java primitives using Jersey REST

Primitives and zero or single-element List array are properly serialized to JSON document. Without customized resolver you don't get proper json[] array fields if Java list is empty. My post lists all the .jar libraries you need. I am using the most recent v1.14 Jersey archive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文