Jersey:找不到 Java 类和 MIME 媒体类型应用程序/json 的消息正文编写器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13439386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey: A message body writer for Java Class and MIME mediatype application/json was not found
提问by dhartwich
after trying to figure out what's my problem I finally decided to ask you how to solve my problem. I've seen different people with the same problem and I tried all the things they were adviced to do but nothing helped with my issue. So basically I'm having a RESTful Service which I build using Jersey. For my client I would like to return an object in JSON Format. I read through different tutorials and decided that it makes sense to use jersey-json-1.8 library. I added everything to my project as usual and tried to run it but each time I'm calling the service (via get request atm.) I'm getting HTTP Error Code 500 (internal server error) and my server responds that no message body writer could be found. If I'm returning XML it works just fine and everything is great. I also tried copying jersey-json-1.8.jar to my Tomcat lib folder because I had to do this with the mysql lib I'm using but it didn't help either. I would be really glad if you could help me out to get this stuff working! If you need any more information just leave a comment and I'll provide it as quickly as humanly possibly :)
在试图弄清楚我的问题是什么之后,我终于决定问你如何解决我的问题。我见过不同的人遇到同样的问题,我尝试了他们被建议做的所有事情,但对我的问题没有任何帮助。所以基本上我有一个使用 Jersey 构建的 RESTful 服务。对于我的客户,我想以 JSON 格式返回一个对象。我阅读了不同的教程,并决定使用 jersey-json-1.8 库是有意义的。我像往常一样将所有内容添加到我的项目中并尝试运行它,但每次我调用服务时(通过 get request atm。)我收到 HTTP 错误代码 500(内部服务器错误)并且我的服务器响应没有消息正文可以找到作者。如果我返回 XML,它工作得很好,一切都很好。我也尝试复制 jersey-json-1.8。jar 到我的 Tomcat lib 文件夹,因为我必须使用我正在使用的 mysql lib 执行此操作,但它也无济于事。如果你能帮我解决这个问题,我会很高兴的!如果您需要更多信息,请发表评论,我会尽快提供给您:)
My project setup is: 3 Different packages 1. My RESTfulServices 2. My Java Work where i handle SQL connections etc. 3. A package where I store all my models that I need to work with and that I want to return in JSON Format (in my example a route for a testdrive)
我的项目设置是: 3 个不同的包 1. 我的 RESTfulServices 2. 我的 Java 工作,我在其中处理 SQL 连接等。 3. 一个我存储我需要使用的所有模型并且我想以 JSON 格式返回的包(在我的示例中,试驾路线)
A Tomcat Webserver IDE: Eclipse I'm using Maven
Tomcat Webserver IDE:Eclipse 我正在使用 Maven
No matter what or how I'm trying to return the Object it just won't work and I'm constantly getting the error message :
无论我如何或如何尝试返回对象,它都无法正常工作,而且我不断收到错误消息:
Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.mykong.models.Teststrecke, and Java type class com.mykong.models.Teststrecke, and MIME media type application/json was not found
EDIT: Here's my JSON Service Method
编辑:这是我的 JSON 服务方法
@Path("/hellojson")
public class JSONService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<Route> getJSONMsg()
{
Route ts = new Route();
ts.setId(1);
ts.setName("HelloWorld");
Route ts2 = new Route();
ts2.setId(2);
ts2.setName("HelloWorld");
ArrayList<Route> availRoutes = new ArrayList<Route>();
availRoutes.add(ts);
availRoutes.add(ts2);
return availRoutes;
}
}
回答by eugen
Try adding Genson library to your classpath http://owlike.github.io/genson/. It is a json<>java streaming and databinding api. It integrates well with jersey, to get you running you need 0 configuration. Jersey detects that the library is in your classpath and enables json mapping by delegating to Genson.
尝试将 Genson 库添加到您的类路径http://owlike.github.io/genson/。它是一个 json<>java 流和数据绑定 API。它与 jersey 很好地集成在一起,要让您运行,您需要 0 配置。Jersey 检测到该库在您的类路径中,并通过委托给 Genson 来启用 json 映射。
回答by borchvm
Include this dependencies in your POM.xml and run Maven -> Update
将此依赖项包含在您的 POM.xml 中并运行 Maven -> Update
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>0.99</version>
</dependency>
回答by nilesh
Adding below dependency solved my issue. As per @eugen I tried adding Gensondependency which helped get rid of the exception, however my server was throwing 400 bad request for some reason. It seems like Gensonwas not able to stream the json request properly. But below dependency worked fine. Hope it helps others as this problem was driving me nuts!
添加以下依赖项解决了我的问题。根据@eugen,我尝试添加Genson依赖项以帮助摆脱异常,但是由于某种原因,我的服务器抛出了 400 个错误的请求。似乎Genson无法正确传输 json 请求。但下面的依赖工作正常。希望它可以帮助其他人,因为这个问题让我发疯!
<dependency>
<groupId>com.fasterxml.Hymanson.jaxrs</groupId>
<artifactId>Hymanson-jaxrs-json-provider</artifactId>
<version>2.3.0</version>
</dependency>
回答by supercobra
Moving jersey-jsondependency to the top of the pom.xml solved this problem for me.
将jersey-json依赖移到 pom.xml 的顶部为我解决了这个问题。
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.18.1</version>
</dependency>
<!-- other dependencies -->
</dependencies>
回答by Anthony
Check under Maven Dependencies in the Package Explorer, if you don't see that or jersey-bundle-1.8.jar, then you probably have specified just jersey-server. Change the dependency in pom.xml to jersey-bundle and it should work.
在 Package Explorer 中的 Maven Dependencies 下检查,如果您没有看到那个或 jersey-bundle-1.8.jar,那么您可能只指定了 jersey-server。将 pom.xml 中的依赖项更改为 jersey-bundle,它应该可以工作。
回答by Jasmine
Jersey 2.4: I faced this problem too and posting this incase it helps someone. I picked this up from the Jersey API Specification for JSON Integration : (https://jersey.java.net/documentation/latest/user-guide.html#json)
Jersey 2.4:我也遇到了这个问题,并发布了这篇文章,以防它对某人有所帮助。我从用于 JSON 集成的 Jersey API 规范中获取了这个:(https://jersey.java.net/documentation/latest/user-guide.html#json)
I used Jettison to integration with Jersey with JSON. But even after adding all the libraries I got the error: A message body writer for Java Class and MIME mediatype application/json was not found
我使用 Jettison 将 Jersey 与 JSON 集成。但即使在添加了所有库后,我还是收到了错误:找不到 Java 类和 MIME 媒体类型应用程序/json 的消息正文编写器
Its all about adding the right libraries and also registering features> I added one extra library in addition to all the others that Jersey provides: jersey-media-json-jettison-2.4.jar. And added the below code to register Jettison Feature.
这一切都是关于添加正确的库并注册功能>除了 Jersey 提供的所有其他库之外,我还添加了一个额外的库:jersey-media-json-jettison-2.4.jar。并添加了以下代码来注册 Jettison Feature。
public class MyApplication extends ResourceConfig{
public MyApplication() {
register(JettisonFeature.class);
packages("org.XXX.XXX.XXX");
}
回答by allenwoot
This question was asked a while ago, but for those reading, note that jersey servlet container has it's own POJO mapper.
这个问题是不久前被问到的,但对于那些阅读,请注意 jersey servlet 容器有它自己的 POJO 映射器。
Enable it by adding the following to your web.xml
通过将以下内容添加到您的 web.xml 来启用它
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
I did notice some discrepancies in how jersey and genson map objects. For example, if a class has a field of type A which is set to an instance of B, which extends A, genson will not properly serialize B, while jersey does.
我确实注意到 jersey 和 genson 映射对象的方式存在一些差异。例如,如果一个类有一个类型为 A 的字段,它被设置为 B 的一个实例,它扩展了 A,genson 将不会正确序列化 B,而 jersey 会。
回答by Prakhar Agrawal
The above issue will be solved by using the Hymanson library instead of the genson.
上述问题将通过使用 Hymanson 库而不是 genson 来解决。
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.Hymanson.core/Hymanson-databind -->
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.25</version>
</dependency>
As in my case genson was giving an empty json in the response but will be fine if we are converting the json to string.But will create an undesirable output because in the response you will get a string not a json.So better to go with the Hymanson library which will do it very easily.
就像在我的情况下,genson 在响应中给出了一个空的 json,但如果我们将 json 转换为字符串就可以了。但是会创建一个不需要的输出,因为在响应中你会得到一个字符串而不是 json。所以最好使用Hymanson 库可以很容易地做到这一点。
Note: Hymanson will convert your response to the json only if the class is implementing the Serializable Interface.So if you are sending JSONObject, then better to send the List of JSONObject.
注意:Hymanson 只会在类实现 Serializable Interface 时将您的响应转换为 json。所以如果您要发送 JSONObject,那么最好发送 JSONObject 的列表。
回答by MankitaP
Remove @Produces(MediaType.APPLICATION_JSON); and use JSONLibraries.zip file to convert your data to and from JSON. It is working perfectly.The problem with above code is that it is using maven and and Hymanson combination. So APPLICATION_JSON cannot be found when you are using only jersey. I worked with to/from JSON conversion stuff, It gave me same error, I tried with this solution and it was working properly.
删除@Produces(MediaType.APPLICATION_JSON); 并使用 JSONLibraries.zip 文件将您的数据与 JSON 相互转换。它运行良好。上面代码的问题是它使用的是 maven 和 Hymanson 组合。因此,当您仅使用球衣时,无法找到 APPLICATION_JSON。我使用到/从 JSON 转换的东西,它给了我同样的错误,我尝试了这个解决方案并且它工作正常。
Code:
代码:
@Path("/hellojson")
public class JSONService {
@GET
public ArrayList<Route> getJSONMsg()
{
Route ts = new Route();
ts.setId(1);
ts.setName("HelloWorld");
Route ts2 = new Route();
ts2.setId(2);
ts2.setName("HelloWorld");
ArrayList<Route> availRoutes = new ArrayList<Route>();
availRoutes.add(ts);
availRoutes.add(ts2);
return JSONObject.fromObject(availRoutes.toString()));
}
}

