Java 严重:找不到 Media type=application/json、type=class com.jersey.jaxb.Todo、genericType=class com.jersey.jaxb.Todo 的 MessageBodyWrit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29136404/
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
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.jersey.jaxb.Todo, genericType=class com.jersey.jaxb.Todo
提问by Raj Hassani
I am trying to create a RESTful web-service and I created one but I am getting a
我正在尝试创建一个 RESTful web 服务,我创建了一个,但我得到了一个
MessageBodyWriter not found for media type=application/json error
MessageBodyWriter not found for media type=application/json 错误
My Todo
class:
我的Todo
班级:
package com.jersey.jaxb;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.pojomatic.Pojomatic;
import org.pojomatic.annotations.AutoProperty;
@XmlRootElement
@XmlType(name = "todo")
@XmlAccessorType(XmlAccessType.FIELD)
@AutoProperty
public class Todo {
@XmlElement(name = "summary")
private final String summary;
@XmlElement(name = "description")
private final String description;
public String getSummary() {
return summary;
}
public String getDescription() {
return description;
}
public Todo() {
this(new Builder());
}
public Todo(Builder builder) {
this.summary = builder.summary;
this.description = builder.description;
}
@Override
public boolean equals(Object o) {
return Pojomatic.equals(this, o);
}
@Override
public int hashCode() {
return Pojomatic.hashCode(this);
}
@Override
public String toString() {
return Pojomatic.toString(this);
}
public static class Builder {
private String description;
private String summary;
public Builder summary(String summary) {
this.summary = summary;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Todo build() {
return new Todo(this);
}
}
}
And my Resource:-
还有我的资源:-
package com.jersey.jaxb;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path("/todo")
public class TodoResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTodo(){
Todo todo = new Todo.Builder().description("My Todo Object").summary("Created").build();
return Response.status(Status.OK).entity(todo).build();
}
}
My web.xml:
我的 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>MyFirstWebService</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.jersey.jaxb</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>
My Libraries:
我的图书馆:
aopalliance-repackaged-2.4.0-b10.jar
asm-debug-all-5.0.2.jar
hk2-api-2.4.0-b10.jar
hk2-locator-2.4.0-b10.jar
hk2-utils-2.4.0-b10.jar
Hymanson-jaxrs-json-provider-2.2.3.jar
javassist-3.18.1-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.4.0-b10.jar
javax.servlet-api-3.0.1.jar
javax.ws.rs-api-2.0.1.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet.jar
jersey-container-servlet-core.jar
jersey-guava-2.17.jar
jersey-media-jaxb.jar
jersey-server.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
When I run this application on Tomcat server and run this : http://localhost:8080/MyFirstWebService/rest/todo
当我在 Tomcat 服务器上运行这个应用程序并运行这个: http://localhost:8080/MyFirstWebService/rest/todo
I get the error:
我收到错误:
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.jersey.jaxb.Todo, genericType=class com.jersey.jaxb.Todo.
严重:找不到 Media type=application/json、type=class com.jersey.jaxb.Todo、genericType=class com.jersey.jaxb.Todo 的 MessageBodyWriter。
采纳答案by Paul Samsotha
You have Hymanson-jaxrs-json-provider
which is a start..
你有Hymanson-jaxrs-json-provider
这是一个开始..
But...
但...
that artifact is still dependent on Jacskon itself, which includes all these artifacts
那件神器仍然依赖于 Jacskon 本身,包括所有这些神器
That's why we use Maven[1](so we don't have to worry about this kind of thing :-). So go find these.
这就是我们使用 Maven [ 1]的原因(所以我们不必担心这种事情:-)。所以去找这些。
Then just add the package to the web.xml, and it should work
然后只需将包添加到 web.xml,它应该可以工作
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
com.jersey.jaxb,
com.fasterxml.Hymanson.jaxrs.json
</param-value>
1.Maven dependency
1.Maven的依赖
<dependency>
<groupId>com.fasterxml.Hymanson.jaxrs</groupId>
<artifactId>Hymanson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
Or use the below Jersey "wrapper" for the above dependency. It will register the Hymanson providers (so we don't need to explicitly register like above), and the Hymanson exception mappers, and start from version 2.17, provides support for Entity Data Filtering.
或者使用下面的 Jersey “wrapper” 来实现上面的依赖。它将注册 Hymanson 提供程序(因此我们不需要像上面那样显式注册)和 Hymanson 异常映射器,从 2.17 版开始,提供对Entity Data Filtering 的支持。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>${jersey2.version}</version>
</dependency>
Note:The fact that we don't have to register anything with the above dependency, is made possible through the Auto-discovery featureof Jersey. If we for some reason disable the auto-discovery, you will want to explicitly register the HymansonFeature
.
注意:事实上,我们不必注册任何具有上述依赖项的内容,这是通过Jersey的自动发现功能实现的。如果我们出于某种原因禁用自动发现,您将需要显式注册HymansonFeature
.
回答by Milson
The solution may be to make ensure that the model classes have a no-argument constructor.
解决方案可能是确保模型类具有无参数构造函数。
And add this dependency on your pom.XML:
并在您的 pom.XML 上添加此依赖项:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
回答by Vimalesh Valiya
- Update Library Jax-RS 2.0 and Jersey 2.5.1(JAX-RS-RI) only
- Return Bean object(todo) not response because while auto generating json or xml it response create issue.
- 仅更新库 Jax-RS 2.0 和 Jersey 2.5.1(JAX-RS-RI)
- 返回 Bean 对象(todo)不响应,因为在自动生成 json 或 xml 时它响应创建问题。
回答by Chandan Kumar
If you already have the jersey-media-moxy dependency added into your pom.xml. Make sure your entity class has the default constructor. I got this issue when I introduced a paramatrized constructor in the model class. Adding the default constructor again worked for me.
如果您已经在 pom.xml 中添加了 jersey-media-moxy 依赖项。确保您的实体类具有默认构造函数。当我在模型类中引入参数化构造函数时,我遇到了这个问题。再次添加默认构造函数对我有用。
回答by zelli79
I had the same issue, i solved it by addind a empty constructor to the class
我有同样的问题,我通过在类中添加一个空的构造函数来解决它
public SandBoxClass(){} //-> solved the issue**
public SandBoxClass(Integer arg1, Integer arg2) {
this.arg1=arg1;
this.arg2=arg2;
}
回答by Ernestas Kardzys
As for me, it helped to register HymansonFeature
:
至于我,它有助于注册HymansonFeature
:
public class App extends ResourceConfig {
public App() {
packages("info.ernestas.simplerest");
register(new HymansonFeature()); // This magical line helped
}
}