7

I am creating an embedded Jetty webapp with Jersey. I do not know how to add Jackson for automatic JSON serde here:

    ServletHolder jerseyServlet = context.addServlet(
       org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    jerseyServlet.setInitParameter(
        ServerProperties.PROVIDER_CLASSNAMES,
        StringUtils.join(
            Arrays.asList(
                HealthCheck.class.getCanonicalName(),
                Rest.class.getCanonicalName()),
            ";"));

    // Create JAX-RS application.
    final Application application = new ResourceConfig()
        .packages("com.example.application")
        .register(JacksonFeature.class);

    // what do I do now to tie this to the ServletHolder?

How do I register this ResourceConfig with the ServletHolder so Jackson with be used where the annotation @Produces(MediaType.APPLICATION_JSON) is used? Here is the full main class for the embedded Jetty application

package com.example.application.web;

import com.example.application.api.HealthCheck;
import com.example.application.api.Rest;
import com.example.application.api.Frontend;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;

import javax.ws.rs.core.Application;
import java.util.Arrays;

public class JettyStarter {

public static void main(String[] args) throws Exception {

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    Server jettyServer = new Server(9090);
    jettyServer.setHandler(context);
    ServletHolder jerseyServlet = context.addServlet(
     org.glassfish.jersey.servlet.ServletContainer.class, "/*");
    jerseyServlet.setInitOrder(0);

    jerseyServlet.setInitParameter(
        ServerProperties.PROVIDER_CLASSNAMES,
        StringUtils.join(
            Arrays.asList(
                HealthCheck.class.getCanonicalName(),
                Rest.class.getCanonicalName()),
            ";"));

    // Create JAX-RS application.
    final Application application = new ResourceConfig()
        .packages("com.example.application")
        .register(JacksonFeature.class);


    try {
        jettyServer.start();
        jettyServer.join();
    } catch (Exception e) {
        System.out.println("Could not start server");
        e.printStackTrace();
    } finally {
        jettyServer.destroy();
    }
}
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
David Williams
  • 8,388
  • 23
  • 83
  • 171

1 Answers1

11

One way is to just wrap the ResourceConfig in an explicit construction of the ServletContainer, as seen here.

Tested with your example

public class RestServer {

    public static void main(String[] args) throws Exception {

        // Create JAX-RS application.
        final ResourceConfig application = new ResourceConfig()
                .packages("jersey.jetty.embedded")
                .register(JacksonFeature.class);

        ServletContextHandler context 
                 = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        Server jettyServer = new Server(9090);
        jettyServer.setHandler(context);
        ServletHolder jerseyServlet = new ServletHolder(new
                org.glassfish.jersey.servlet.ServletContainer(application));
        jerseyServlet.setInitOrder(0);

        context.addServlet(jerseyServlet, "/*");

        // ... removed property (init-param) to compile. 

        try {
            jettyServer.start();
            jettyServer.join();
        } catch (Exception e) {
            System.out.println("Could not start server");
            e.printStackTrace();
        } finally {
            jettyServer.destroy();
        }
    }
}

You could also...

without changing anything else in your original post, just set the init param to scan the Jackson provider package

jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES,
        "com.fasterxml.jackson.jaxrs.json;"
      + "jersey.jetty.embedded"  // my package(s)
);

Note your attempted use of ResourceConfig seems a little redundant, as you are already configuring your classes in the the init param. You could alternatively get rid of adding each class explicitly and just scan entire packages as I have done.

You could also...

just use the Jackson provider classes you need. You can look in the jar, and you will see more than just the marshalling/unmarhalling provider (Jackson[JAXB]JsonProvider), like a ExceptionMappers. You may not like these mappers and wand to configure your own. In which case, like I said, just include the provider you need. For example

jerseyServlet.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES,
      "com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider");

jerseyServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES, 
      "jersey.jetty.embedded"  // my package(s)
);

After further testing...

Not sure what version of Jersey, but I am using Jersey 2.15 (with jersey-media-json-jackson:2.15), and without any further configuration from just scanning my package for my resource classes, the Jackson feature is already enabled. This is part of the auto discoverable features. I believe this was enable as of 2.8 or 2.9 for the Jackson feature. So if you are using a later one, I don't think you need to explicitly configure anything, at least from what I've tested :-)


UPDATE

All of the above examples have been tested with the below Maven pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.underdog.jersey</groupId>
    <artifactId>jersey-jetty-embedded</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <jersey.version>2.15</jersey.version>
        <jetty.version>9.2.6.v20141205</jetty.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
        </dependency>
        <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.eclipse.jetty</groupId>
            <artifactId>jetty-servlets</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>

        <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

And resource class

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/json")
public class JsonResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getJson() {
        Resource resource = new Resource();
        resource.hello = "world";
        return Response.ok(resource).build();
    }

    public static class Resource {
        public String hello;
    }
}

Using path

http://localhost:9090/json

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Hi, and wow, thank you for such an extensive and detailed answer with many alternatives. I am starting with the first approach, using Jersey 2.10 (my server would not start with versions >= 2.11) I am working on trying the first approach, but after the change I get 404 on all my jaxrs routes. Here is what my Java looks like: http://pastebin.com/4t4rZz9X I left the snippet in that you commented above as `// ... removed property (init-param) to compile.` Am I doing something wrong in that pastebin? – David Williams Feb 26 '15 at 20:13
  • 1
    I just used `htttp://localhost:9090/hello`, where `"hello"` is the `@Path` on a resource class. Also `packages` on `ResourceConfig` scans the entire package and sub-packages for annotated classes. Personally I've never really used adding the classes individually, so I don't know if it in any way conflicts with the package scanning. Try to do one of the other. I have no idea why 2.11+ is not working. I would need to see the stacktrace. "Doesn't start" doesn't say much. Also posting your pom.xml would help also. – Paul Samsotha Feb 26 '15 at 21:49
  • 1
    I will post my pom dependencies for you to see – Paul Samsotha Feb 26 '15 at 21:50
  • Thank you. Here is my pom. Sorry for the vague description, there was no stack or anything, Jersey just never initialized. http://pastebin.com/FpA2Vn2x – David Williams Feb 26 '15 at 22:23
  • Hey any chance you can share your whole app with me? That would be super useful! Maybe on github? Thank you for all your time and sorry to ask for so much of it. – David Williams Feb 26 '15 at 22:24
  • 1
    1) Try it with my dependencies. 2) Try and comment out the init param. It's redundant, if you are already scanning the package with `ResourceConfg.packages` – Paul Samsotha Feb 26 '15 at 22:26