找不到针对媒体type = application / json,type = class java.util.ArrayList的MessageBodyWriter

时间:2018-10-24 12:03:36

标签: java json jackson jetty

非常奇怪的问题。我已经尝试了在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
  }

服务功能都返回ArrayList或基于GenericList的响应:

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

编辑:我找到了解决方法here

注册功能

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

2 个答案:

答案 0 :(得分:1)

确保已安装以下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

答案 1 :(得分:0)

可能您没有为课程现货

定义吸气剂和吸气剂