How to create a simple JSON Jersey 2.x RESTful web service on Glassfish 4?
I spent days trying to figure this out. All I want to do is create a simple web service to return POJO lists as JSON. Why is it so hard?
I start with this in my POM:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.17</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
Here is my ResourceConfig:
@ApplicationPath("reservations")
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
packages("com.oracle.swr.ws.mavenproject3",
"com.fasterxml.jackson.jaxrs.base");
}
}
Here is my resource:
@Path("/bookedAssets")
public class GenericResource {
@Context
private UriInfo context;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<String> getJson() {
List<String> strings = new ArrayList<>();
strings.add("test");
strings.add("test2");
return strings;
}
}
When I try to run it I get:
Heavy: MessageBodyWriter not found for media type = application / json, type = class java.util.ArrayList, genericType = class java.util.ArrayList.
I changed my ResourceConfig to try and use JacksonFeature:
public ApplicationConfig() {
super(GenericResource.class, JacksonFeature.class);
}
And I am getting this error message:
Warning: StandardWrapperValve [com.oracle.swr.ws.mavenproject3.ApplicationConfig]: Servlet.service () for the com.oracle.swr.ws.mavenproject3.ApplicationConfig servlet throws org.jboss.weld.exceptions.UnsatisfiedResolutionException: WnsatisfiedResolutionException: W308 : Cannot resolve any beans to types: [org.glassfish.jersey.message.filtering.spi.ObjectProvider]; Bindings: [QualifierInstance {annotationclass = interface javax.enterprise.inject.Default, values = {}, hashCode = 633679645}]
I've tried many different combinations of things in my POM. Bad luck. I have read the documentation in Jersey. Bad luck.
This guy says the Jersey documentation is crap and instead: http://jersey.576304.n2.nabble.com/Beware-of-JacksonFeature-in-Jersey-td7581633.html . I tried it. Bad luck.
Does anyone have a really simple example of getting GF 4.x, Jersey 2.x, Jackson 2.x?
source to share
(per comment from @peeskillet) I replaced this:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
from:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.5.3</version>
</dependency>
and
packages("com.fasterxml.jackson.jaxrs.base");
from:
packages("com.fasterxml.jackson.jaxrs.json");
source to share
I would recommend using DropWizard instead of using Jersey directly. DropWizard integrates things (Jersey, Jackson, logging, etc) and customization is a breeze. Alternatively, you might consider Spring Boot , unless using Jersey is a strict requirement.
source to share