![](/img/trans.png)
[英]RestEasy: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/json
[英]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.