繁体   English   中英

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

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

很奇怪的问题。 我已经尝试了我在 Stackoverflow 中找到的所有选项,但没有一个有效。

问题是我有一个使用 Jetty 开发的 REST web 服务,它以 JSON 格式返回我的对象​​列表。

我要返回的对象如下所示:

  @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
  }

和服务功能,都返回基于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();

}

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

在 Windows 的 Eclipse 中开发和运行应用程序时,一切正常,但在 Ubuntu 中部署并运行应用程序时,我得到

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

知道为什么会这样吗? 我在 Windows 中使用 Oracle JDK,而在 Linux 中我使用 OpenJDK。 双方都是同一个版本1.8.0_181。

编辑:我在这里找到了解决方案:

注册功能

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM