Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty + Jersey + Jackson, different behavior in Eclipse (Success) vs command line (Error Unsupported Media Type)!

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?

like image 740
Whimusical Avatar asked Jan 15 '15 19:01

Whimusical


1 Answers

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);
like image 164
gihan-maduranga Avatar answered Oct 24 '22 20:10

gihan-maduranga