简体   繁体   English

找不到媒体类型=应用程序/json,类型=类 java.util.ArrayList 的 MessageBodyWriter

[英]MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList

Quite strange issue.很奇怪的问题。 I have tried all options I've found in Stackoverflow but none is working.我已经尝试了我在 Stackoverflow 中找到的所有选项,但没有一个有效。

The issue is I have a REST webservice developed using Jetty and it is returning a list of my objects in JSON format.问题是我有一个使用 Jetty 开发的 REST web 服务,它以 JSON 格式返回我的对象​​列表。

The object I'm returning looks like this:我要返回的对象如下所示:

  @JsonIgnoreProperties(ignoreUnknown = true)
  public class Spot {
    @JsonProperty("id")
    @JsonInclude(Include.NON_NULL)
    private String id;
    @JsonProperty("presenceStatus")
    private boolean status;
    @JsonProperty("longitude")
    private double longitude;
    @JsonProperty("latitude")
    private double latitude;

    public Spot() {
    }

    public Spot(String id, String status, double longitude,
        double latitude) {
      setId(id);
      setStatus(status);
      setLongitude(longitude);
      setLatitude(latitude);
    }

    // Setters and Getters
  }

And the service functions, both returning ArrayList or Response based in GenericList :和服务功能,都返回基于GenericList ArrayList或 Response :

@GET
@Path("listArray")
@Produces(MediaType.APPLICATION_JSON)
public List<Spot> getListArray() {
  List<Spot> spots = functionRetrievingStuf();
  return spots;
}


@GET
@Path("listResponse")
@Produces(MediaType.APPLICATION_JSON)
public Response getListResponse() {
  List<Spot> spots = functionRetrievingStuf();

  GenericEntity<List<Spot>> result =
      new GenericEntity<List<Spot>>(spots) {
      };

  return Response.ok(result).build();

}

My POM file dependencies are:我的 POM 文件依赖项是:

<properties>    
  <jetty.version>9.4.12.v20180830</jetty.version>
  <jersey.version>2.27</jersey.version>
</properties>

<dependencies>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-server</artifactId>
  <version>${jetty.version}</version>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet</artifactId>
  <version>${jetty.version}</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>${jersey.version}</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-jetty-servlet</artifactId>
  <version>${jersey.version}</version>
  <scope></scope>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.media</groupId>
  <artifactId>jersey-media-json-jackson</artifactId>
  <version>${jersey.version}</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-common</artifactId>
  <version>${jersey.version}</version>
</dependency>
<!-- Starting from Jersey 2.26, Jersey removed HK2 as a hard dependency. 
  It created an SPI as an abstraction for the dependency injection provider, 
  in the form of InjectionManager and InjectionManagerFactory. So for Jersey 
  to run, we need to have an implementation of the InjectionManagerFactory. 
  There are two implementations of this, which are for HK2 and CDI. The HK2 
  dependency is the jersey-hk2 others are talking about. -->
<dependency>
  <groupId>org.glassfish.jersey.inject</groupId>
  <artifactId>jersey-hk2</artifactId>
  <version>${jersey.version}</version>
</dependency>
</dependencies>

When developing and running the app in Eclipse in Windows, everything is working fine, but when deploying in Ubuntu and running the app I get the在 Windows 的 Eclipse 中开发和运行应用程序时,一切正常,但在 Ubuntu 中部署并运行应用程序时,我得到

MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList genericType=java.util.List<mypackage.Spot>

Any idea why this is happening?知道为什么会这样吗? I'm using Oracle JDK in Windows, while in Linux I'm using OpenJDK.我在 Windows 中使用 Oracle JDK,而在 Linux 中我使用 OpenJDK。 Both sides are the same version 1.8.0_181.双方都是同一个版本1.8.0_181。

Edit: I found the solution here :编辑:我在这里找到了解决方案:

Register the feature注册功能

jerseyServlet.setInitParameter(
    "jersey.config.server.provider.classnames",
    "org.glassfish.jersey.jackson.JacksonFeature");

确保您有以下 JARS:1) jackson-core-asl-1.9.13 2) jackson-jaxrs-1.9.13 3) jackson-mapper-asl-1.9.13 4) jackson-xc-1.9.13

可能你还没有为你的类Spot定义 getter 和 setter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RestEasy:找不到类型的响应对象的MessageBodyWriter:java.util.Array媒体类型列表:application / json - RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json 未找到 Media type=text/plain、type=class java.util.ArrayList、genericType=java.util.List 的 MessageBodyWriter<models.Person> - MessageBodyWriter not found for media type=text/plain, type=class java.util.ArrayList, genericType=java.util.List<models.Person> 严重:未找到 Java 类 java.util.ArrayList 和 MIME 媒体类型 application/json 的消息正文编写器 - SEVERE: A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList and MIME media type application/json was not found 找不到Java类java.util.ArrayList和Java类型类java.util.ArrayList和MIME媒体类型application / json的消息正文编写器 - A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/json was not found 找不到Java类型类java.util.ArrayList和MIME媒体类型application / xml的消息正文编写器 - A message body writer for Java type, class java.util.ArrayList, and MIME media type, application/xml, was not found 找不到响应 object 类型的 MessageBodyWriter:java.util.ArrayList 媒体类型:text/html - 在 Resteasy 中 - Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: text/html - in Resteasy 未找到Java类java.util.ArrayList ...和MIME媒体类型text / xml的消息正文编写器 - A message body writer for Java class java.util.ArrayList…and MIME media type text/xml was not found 找不到媒体类型 = 应用程序/xml,类型 = 类 java.util.HashMap$Values 的 MessageBodyWriter - MessageBodyWriter not found for media type=application/xml, type=class java.util.HashMap$Values 未找到类型为 java.util.ArrayList 的返回值的转换器 - No converter found for return value of type: class java.util.ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM