6

I'm getting crazy trying to run Jetty Jersey and Jackson outside Eclipse.

I have a main class:

public class Main {

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

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*").setInitParameter(
            "jersey.config.server.provider.classnames", CanaryEndpoint.class.getCanonicalName());

        Server jettyServer = new org.eclipse.jetty.server.Server(8089);
        jettyServer.setHandler(context);
        jettyServer.start();
        jettyServer.join();
    }
}

An endpoint class:

@Path("/endpoint")
public class CanaryEndpoint {

    @POST
    @Path("/test")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String canaryTest(ValueWrapper param) {
        System.out.println("Deserialized Property: " + param.isFlag());
        return "{OK}";
    }
}

and an Entity (to unserialize) class:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class ValueWrapper {
    @XmlElement
    public boolean flag;

    public ValueWrapper() {}
    public boolean isFlag()           { return flag; }
    public void setFlag(boolean flag) { this.flag = flag;}
}

Now it comes the funny part. I use Postman to test the JSON consuming by sending a POST petition to 8089 port and a raw JSON value of {"flag" : "true"} with both Content-type and Accept adequately set.

When I play Main class through eclipse server answers correctly, but when I do it in the CLI (mvn clean package + java -cpJar main_class), server answers Error 415: Unsuported Media Type

My simple pom.xml:

<jersey.version>2.14</jersey.version>
<jetty-version>9.2.6.v20141205</jetty-version>
(...)
<artifactId>jetty-server</artifactId>
<artifactId>jetty-servlet</artifactId>
<artifactId>jersey-container-servlet-core</artifactId>
<artifactId>jersey-json</artifactId>
<artifactId>jersey-media-json-jackson</artifactId>

Any suggestion regarding what is happening?

EDIT: I narrowed a bit the problem. Seems like com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider is not registering when the app is executed from the console. How can I manually register it?

Whimusical
  • 6,401
  • 11
  • 62
  • 105

2 Answers2

3

I had the same problem when executing jersey rest services in uber jar.I was able to fix this using following article.Hope this might help someone.JSON example with Jersey + Jackson

Above example was written in jersey 1.x.Here is the jersey 2.x implementation.

Add following jersey jackson dependency to your pom

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
     <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.21</version>
</dependency>

And add following init-param in web.xml

<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.jackson.JacksonFeature</param-value>
</init-param>

or you can explicitly register the jackson feature like @peeskillet mentioned.

ResourceConfig config = new ResourceConfig();
config.register(JacksonFeature.class);
gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74
2

I'm not really sure what the difference is between running from the IDE and the jar from command line, but I was able to reproduce the same problem. I tried to use your code above, and also the code from this answer

public class TestJerseyServer {
    public static void main(String[] args) throws Exception {
        ResourceConfig config = new ResourceConfig();
        config.packages("jetty.practice.resources");
        ServletHolder jerseyServlet 
                        = new ServletHolder(new ServletContainer(config));

        Server server = new Server(8080);
        ServletContextHandler context 
                = new ServletContextHandler(server, "/");
        context.addServlet(jerseyServlet, "/*");
        server.start();
        server.join();
    }
}

The only way I was able to get it work was with my code example, but explicitly registering the JacksonFeature

ResourceConfig config = new ResourceConfig();
config.register(JacksonFeature.class);

I even tried to set an init-param to scan for the Jackson package, that didn't work either.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720