gpt4 book ai didi

java - 严重 : MessageBodyWriter not found for media type=application/json, 类型=com.jersey.jaxb.Todo 类,genericType=com.jersey.jaxb.Todo 类

转载 作者:IT老高 更新时间:2023-10-28 12:49:19 26 4
gpt4 key购买 nike

我正在尝试创建一个 RESTful Web 服务,我创建了一个,但我得到了一个

MessageBodyWriter not found for media type=application/json error

我的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);
}
}
}

还有我的资源:-

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();
}

}

我的 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>

我的图书馆:

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
jackson-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

当我在 Tomcat 服务器上运行这个应用程序并运行它时: http://localhost:8080/MyFirstWebService/rest/todo

我得到错误:

SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.jersey.jaxb.Todo, genericType=class com.jersey.jaxb.Todo.

最佳答案

你有 jackson-jaxrs-json-provider 这是一个开始..

但是...

该工件仍然依赖于 Jacskon 本身,其中包括所有这些工件

enter image description here

这就是我们使用 Maven[1] 的原因(所以我们不必担心这种事情 :-)。所以去找这些。

然后只需将包添加到 web.xml,它应该可以工作

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

1. Maven 依赖

<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>

或者为上述依赖使用下面的 Jersey “包装器”。它将注册 Jackson 提供程序(因此我们不需要像上面那样显式注册)和 Jackson 异常映射器,从 2.17 版本开始,提供对 Entity Data Filtering 的支持.

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>

注意:我们不必注册任何具有上述依赖关系的东西,这可以通过Auto-discovery feature 来实现。 Jersey 。如果我们出于某种原因禁用自动发现,您将需要显式注册 JacksonFeature

关于java - 严重 : MessageBodyWriter not found for media type=application/json, 类型=com.jersey.jaxb.Todo 类,genericType=com.jersey.jaxb.Todo 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136404/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com