Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a simple JSON Jersey 2.x RESTful web service on Glassfish 4?

I've spent days trying to figure this out. All I want to do is create a simple web service to return Lists of POJOs as JSON. Why is this so difficult?

I'm starting 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:

Severe: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.

I modified my ResourceConfig to try to use JacksonFeature:

public ApplicationConfig() {
    super(GenericResource.class, JacksonFeature.class);
}

And I get this error message:

Warning: StandardWrapperValve[com.oracle.swr.ws.mavenproject3.ApplicationConfig]: Servlet.service() for servlet com.oracle.swr.ws.mavenproject3.ApplicationConfig threw exception org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001308: Unable to resolve any beans for 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. No luck. I've read the Jersey documentation. No luck.

This guy says the Jersey documentation is crap and to do this instead: http://jersey.576304.n2.nabble.com/Beware-of-JacksonFeature-in-Jersey-td7581633.html. Tried it. No luck.

Does anyone have a really simple example of getting GF 4.x, Jersey 2.x, Jackson 2.x working?

like image 703
wsaxton Avatar asked Nov 18 '25 13:11

wsaxton


1 Answers

(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>

with:

<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");

with:

packages("com.fasterxml.jackson.jaxrs.json");
like image 85
wsaxton Avatar answered Nov 21 '25 05:11

wsaxton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!