Reputation: 1258
Quite strange issue. I have tried all options I've found in Stackoverflow but none is working.
The issue is I have a REST webservice developed using Jetty and it is returning a list of my objects in JSON format.
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
:
@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:
<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
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. Both sides are the same version 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");
Upvotes: 0
Views: 2859
Reputation: 847
Ensure that you have following JARS in place: 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
Upvotes: 1
Reputation: 29
Probably you have not defined the getters and setters for your class Spot
Upvotes: 0