MessageBodyWriter not found for media type=application/json,



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

问题是我有一个使用 Jetty 开发的 REST 网络服务,它以 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或响应:

@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( 杰克逊核心 ASL-1.9.13 2( 杰克逊-JAXRS-1.9.13 3( 杰克逊-映射器-ASL-1.9.134( 杰克逊-XC-1.9.13

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

spot

相关内容

最新更新