严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

2022-01-21 00:00:00 json rest java jax-rs jersey

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

<块引用>

找不到媒体类型=应用程序/json错误的MessageBodyWriter

我的Todo类:

包com.jersey.jaxb;导入 javax.xml.bind.annotation.XmlAccessType;导入 javax.xml.bind.annotation.XmlAccessorType;导入 javax.xml.bind.annotation.XmlElement;导入 javax.xml.bind.annotation.XmlRootElement;导入 javax.xml.bind.annotation.XmlType;导入 org.pojomatic.Pojomatic;导入 org.pojomatic.annotations.AutoProperty;@XmlRootElement@XmlType(name = "todo")@XmlAccessorType(XmlAccessType.FIELD)@AutoProperty公共类待办事项{@XmlElement(名称=摘要")私有最终字符串摘要;@XmlElement(name = "描述")私有最终字符串描述;公共字符串 getSummary() {返回摘要;}公共字符串 getDescription() {返回说明;}公共待办事项(){这(新的建造者());}公共待办事项(建设者建设者){this.summary = builder.summary;this.description = builder.description;}@覆盖公共布尔等于(对象o){返回 Pojomatic.equals(this, o);}@覆盖公共 int hashCode() {返回 Pojomatic.hashCode(this);}@覆盖公共字符串 toString() {返回 Pojomatic.toString(this);}公共静态类生成器{私有字符串描述;私有字符串摘要;公共生成器摘要(字符串摘要){this.summary = 摘要;返回这个;}公共构建器描述(字符串描述){this.description = 描述;返回这个;}公共待办事项构建(){返回新的待办事项(这个);}}}

还有我的资源:-

包com.jersey.jaxb;导入 javax.ws.rs.Path;导入 javax.ws.rs.core.MediaType;导入 javax.ws.rs.Produces;导入 javax.ws.rs.GET;导入 javax.ws.rs.core.Response;导入 javax.ws.rs.core.Response.Status;@Path("/待办事项")公共类 TodoResource {@得到@Produces(MediaType.APPLICATION_JSON)公共响应 getTodo(){Todo todo = new Todo.Builder().description("我的 Todo 对象").summary("Created").build();返回 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"><欢迎文件列表><欢迎文件>index.html</欢迎文件><欢迎文件>index.htm</欢迎文件><欢迎文件>index.jsp</欢迎文件><欢迎文件>default.html</欢迎文件><欢迎文件>default.htm</欢迎文件><欢迎文件>default.jsp</欢迎文件></欢迎文件列表><显示名称>MyFirstWebService</显示名称><小服务程序><servlet-name>Jersey REST 服务</servlet-name><servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class><初始化参数><param-name>jersey.config.server.provider.packages</param-name><参数值>com.jersey.jaxb</参数值></初始化参数><启动时加载>1</启动时加载></servlet><servlet 映射><servlet-name>Jersey REST 服务</servlet-name><url-pattern>/rest/*</url-pattern></servlet 映射></网络应用>

我的图书馆:

aopalliance-repackaged-2.4.0-b10.jarasm-debug-all-5.0.2.jarhk2-api-2.4.0-b10.jarhk2-locator-2.4.0-b10.jarhk2-utils-2.4.0-b10.jar杰克逊-jaxrs-json-provider-2.2.3.jarjavassist-3.18.1-GA.jarjavax.annotation-api-1.2.jarjavax.inject-2.4.0-b10.jarjavax.servlet-api-3.0.1.jarjavax.ws.rs-api-2.0.1.jarjaxb-api-2.2.7.jarjersey-client.jarjersey-common.jar球衣容器-servlet.jarjersey-container-servlet-core.jarjersey-guava-2.17.jar泽西-媒体-jaxb.jarjersey-server.jarorg.osgi.core-4.2.0.jarosgi-资源定位器-1.0.1.jar持久性-api-1.0.jar验证-api-1.1.0.Final.jar

当我在 Tomcat 服务器上运行这个应用程序并运行它时:.

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

注意: 我们不必注册任何具有上述依赖项的东西,这是通过 泽西岛的自动发现功能.如果我们出于某种原因禁用自动发现,您将需要显式注册 JacksonFeature.

I am trying to create a RESTful web-service and I created one but I am getting a

MessageBodyWriter not found for media type=application/json error

My Todo class:

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:

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

When I run this application on Tomcat server and run this : 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.

解决方案

You have jackson-jaxrs-json-provider which is a start..

But...

that artifact is still dependent on Jacskon itself, which includes all these artifacts

That's why we use Maven[1] (so we don't have to worry about this kind of thing :-). So go find these.

Then just add the package to the web.xml, and it should work

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


1. Maven dependency

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

Or use the below Jersey "wrapper" for the above dependency. It will register the Jackson providers (so we don't need to explicitly register like above), and the Jackson exception mappers, and start from version 2.17, provides support for Entity Data Filtering.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</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 feature of Jersey. If we for some reason disable the auto-discovery, you will want to explicitly register the JacksonFeature.

相关文章