使用Jersey时未生成JSON

编程入门 行业动态 更新时间:2024-10-21 14:21:49
本文介绍了使用Jersey时未生成JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用最新版本的Jersey来实现一些REST示例服务。 你有什么线索为什么当我使用Fiddler生成以下HTTP请求时,我得到: 500内部服务器错误

I'm using the latest version of Jersey to implement some REST sample services. Do you have any clue why when I generate the following HTTP request using Fiddler,I get: 500 Internal Server Error

MessageBodyWriter not found for media type={application/json, q=1000}, type=class java.util.ArrayList, genericType=java.util.ArrayList<com.example.Todo>

HTTP请求:

GET localhost:8080/RestProject/rest/todos/1 HTTP/1.1 Connection: close Accept: application/json User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2272.101 Safari/537.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4 Host: localhost:8080

这是被调用的Rest方法:

That's the Rest method getting called:

//This method is called if XML or JSON is requested @GET @Path("{id}") @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON,MediaType.TEXT_XML}) public Todo getEntityXMLOrJSON(@PathParam("id") int id) { Todo todo = new Todo(); todo.setSummary("This is my first todo " + id); todo.setDescription("This is my first todo"); return todo; }

当我请求xml数据时,一切正常。

When I request xml data everything works fine.

编辑:

推荐答案

我对Vogella教程非常熟悉。很多人发布了相关信息。它使用Jersey 2.您正在尝试将一些Jersey 1罐子添加到项目中。拿出你自己添加的所有内容。然后查找并添加这些

I'm pretty familiar with the Vogella tutorial. So many people have posted about it. It uses Jersey 2. You are trying to add some Jersey 1 jars to the project. Take all the ones you added on your own out. Then find and add these

如果你正在使用web.xml,那么就像这样注册提供者

And if you're using web.xml, then register the provider like so

<param-name>jersey.config.server.provider.packages</param-name> <param-value> com.jersey.jaxb,com.fasterxml.jackson.jaxrs.json </param-value>

向jersey servlet添加init参数。否则,使用Java配置,在 ResourceConfig 中,只需

adding an init param to the jersey servlet. Otherwise, using Java config, in your ResourceConfig, just

register(JacksonJaxbJsonProvider.class);

这适用于JAXB注释支持。如果你不需要它,那么你可以使用 JacksonJsonProvider

That's for JAXB annotation support. If you don't need it, then you can just use JacksonJsonProvider

更多推荐

使用Jersey时未生成JSON

本文发布于:2023-10-31 06:18:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Jersey   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!