Java 找不到 Media type=application/json 的 MessageBodyWriter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26207252/
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
MessageBodyWriter not found for media type=application/json
提问by Fahad Mullaji
I am facing issues while consuming JAX-RS services as JSON.
我在将 JAX-RS 服务作为 JSON 使用时遇到问题。
Below I have added my code.
下面我添加了我的代码。
This is my service class:
这是我的服务类:
//Sets the path to base URL + /hello
@Path("/hello")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Hello {
@GET
@Produces("application/json")
public Student getStudent() {
Student s = new Student();
s.first_name = "Test First Name !!!";
s.last_name = "Test Last Name!!!";
return s;
}
Student
class which I am trying to get from service:
Student
我试图从服务中获得的课程:
@XmlRootElement
public class Student implements Serializable {
public String first_name;
public String last_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public Student()
{
first_name = "Fahad";
last_name = "Mullaji";
}
}
Web XML on service side.
服务端的 Web XML。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
I don't know how to fix this issue. I am using SOAP UI for testing JSON response but I guess that it should not matter.
我不知道如何解决这个问题。我正在使用 SOAP UI 来测试 JSON 响应,但我想这应该无关紧要。
Many places I read that I need to add the code below. But I don't know where. I am not using Maven for building.
我读到的很多地方都需要在下面添加代码。但我不知道在哪里。我没有使用 Maven 进行构建。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
Server is looking for function to parse Student
object to JSON but it is not able to find function or jar file for it.
I have added jar of Genson, Moxy, RESTEasy and Hymanson but I think that's the problem.
I think I am missing mapping somewhere.
服务器正在寻找将Student
对象解析为 JSON 的函数,但无法为其找到函数或 jar 文件。我添加了 jar 的 Genson、Moxy、RESTEasy 和 Hymanson,但我认为这就是问题所在。我想我在某处缺少映射。
回答by Alicia Tang
I was able to fix it by install jersey-media-json-Hymanson
我能够通过安装 jersey-media-json-Hymanson 来修复它
Add the dependency to pom.xml
将依赖添加到 pom.xml
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<scope>runtime</scope>
</dependency>
回答by amess
I was in the same situation where
- I was notusing Maven or Ant,
- I finished this Vogella tutorial on Jersey,
- and I was getting the MessageBodyWriter
error when trying to use @Produces(MediaType.APPLICATION_JSON)
.
我处于同样的情况
- 我没有使用 Maven 或 Ant,
- 我在 Jersey 上完成了这个 Vogella 教程,
- 我MessageBodyWriter
在尝试使用@Produces(MediaType.APPLICATION_JSON)
.
This answerby @peeskillet solves the problem - you have to use the Hymanson *.jar files that are available from the FasterXML Hymanson Download page.You'll need the core
files as well as the jaxrs
files.
@peeskillet 的这个答案解决了这个问题 -您必须使用FasterXML Hymanson 下载页面中提供的 Hymanson *.jar 文件。您将需要这些core
文件以及这些jaxrs
文件。
I added them to my WebContent/WEB-INF/lib
folder where I have my Jersey *.jar files per the above tutorial, and made the small change to the web.xml file below (again, as originally shared by @peeskillet):
WebContent/WEB-INF/lib
根据上述教程,我将它们添加到我的Jersey *.jar 文件的文件夹中,并对下面的 web.xml 文件进行了小的更改(同样,最初由 @peeskillet 共享):
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
your.other.package.here, com.fasterxml.Hymanson.jaxrs.json
</param-value>
The important part being com.fasterxml.Hymanson.jaxrs.json
.
重要的部分是com.fasterxml.Hymanson.jaxrs.json
.
回答by Saurabh Thakur
Uncommenting the below code helped
取消注释以下代码有帮助
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
which was present in pom.xml in my maven based project resolved this error for me.
在我的基于 maven 的项目中存在于 pom.xml 中,为我解决了这个错误。
回答by asad.qazi
You've to create empty constructor because JAX-RS initializes the classes... Your constructor must have no arguments:
您必须创建空构造函数,因为 JAX-RS 会初始化这些类……您的构造函数必须没有参数:
@XmlRootElement
public class Student implements Serializable {
public String first_name;
public String last_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public Student()
{
first_name = "Fahad";
last_name = "Mullaji";
}
public Student()
{
}
}
回答by sajan
I think may be you should try to convert the data to json format. It can be done by using gson lib. Try something like below.
我想可能是您应该尝试将数据转换为 json 格式。它可以通过使用 gson lib 来完成。尝试类似下面的内容。
I don't know it is a best solutions or not but you could do it this way.It worked for me
我不知道这是否是最好的解决方案,但你可以这样做。它对我有用
example : `
示例:`
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response info(@HeaderParam("Accept") String accept) {
Info information = new Info();
information.setInfo("Calc Application Info");
System.out.println(accept);
if (!accept.equals(MediaType.APPLICATION_JSON)) {
return Response.status(Status.ACCEPTED).entity(information).build();
} else {
Gson jsonConverter = new GsonBuilder().create();
return Response.status(Status.ACCEPTED).entity(jsonConverter.toJson(information)).build();
}
}
回答by omilus
Below should be in your pom.xml above other jersy/Hymanson dependencies. In my case it as below jersy-client dep-cy and i got MessageBodyWriter not found for media type=application/json.
下面应该在你的 pom.xml 中其他 jersy/Hymanson 依赖项之上。在我的情况下,它如下 jersy-client dep-cy 并且我没有找到 Media type=application/json 的 MessageBodyWriter。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.25</version>
</dependency>
回答by Dulith De Costa
To overcome this issue try the following. Worked for me.
要克服此问题,请尝试以下操作。为我工作。
Add following dependency in the pom.xml
在pom.xml 中添加以下依赖项
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.25</version>
</dependency>
And make sure Model classcontains no arg constructor
并确保Model 类不包含arg 构造函数
public Student()
{
}
回答by JediCate
In my experience this error is pretty common, for some reason jersey sometimes has problems parsing custom java types. Usually all you have to do is make sure that you respect the following 3 conditions:
根据我的经验,这个错误很常见,由于某种原因,球衣有时在解析自定义 Java 类型时会出现问题。通常,您所要做的就是确保遵守以下 3 个条件:
- you have jersey-media-json-Hymanson in you pom.xml if using maven or added to your build path;
- you have an empty constructor in the data type you are trying to de-/serialize;
- you have the relevant annotation at the class and field level for your custom data type (xmlelement and/or jsonproperty);
- 如果使用 maven 或添加到构建路径,则 pom.xml 中有 jersey-media-json-Hymanson;
- 您尝试反/序列化的数据类型中有一个空的构造函数;
- 您在自定义数据类型(xmlelement 和/或 jsonproperty)的类和字段级别具有相关注释;
However, I have ran into cases where this just was not enough. Then you can always wrap you custom data type in a GenericEntity and pass it as such to your ResponseBuilder:
但是,我遇到过这还不够的情况。然后,您始终可以将自定义数据类型包装在 GenericEntity 中,并将其传递给您的 ResponseBuilder:
GenericEntity<CustomDataType> entity = new GenericEntity<CustomDataType>(myObj) {};
return Response.status(httpCode).entity(entity).build();
This way you are trying to help jersey to find the proper/relevant serialization provider for you object. Well, sometimes this also is not enough. In my case I was trying to produce a text/plain from a custom data type. Theoretically jersey should have used the StringMessageProvider, but for some reason that I did not manage to discover it was giving me this error:
通过这种方式,您可以尝试帮助 jersey 为您的对象找到合适/相关的序列化提供程序。好吧,有时这也是不够的。就我而言,我试图从自定义数据类型生成文本/纯文本。理论上 jersey 应该使用 StringMessageProvider,但由于某种原因,我没有发现它给了我这个错误:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain
So what solved the problem for me was to do my own serialization with Hymanson's writeValueAsString(). I'm not proud of it but at the end of the day I can deliver an acceptable solution.
所以对我来说解决这个问题的是用Hyman逊的 writeValueAsString() 做我自己的序列化。我并不为此感到自豪,但最终我可以提供可接受的解决方案。
回答by Jainender Chauhan
Ensure that you have following JARS in place: 1) Hymanson-core-asl-1.9.13 2) Hymanson-jaxrs-1.9.13 3) Hymanson-mapper-asl-1.9.13 4) Hymanson-xc-1.9.13
确保您有以下 JARS:1) Hymanson-core-asl-1.9.13 2) Hymanson-jaxrs-1.9.13 3) Hymanson-mapper-asl-1.9.13 4) Hymanson-xc-1.9.13