Jetty + Jersey + Jackson, other behavior in Eclipse (Success) and command line (error of unsupported media type)!

I'm going crazy trying to run Jetty Jersey and Jackson outside of 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(); } } 

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 class Entity (to unserialize):

 @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 this is the funny part. I use Postman to test JSON consumption by sending a POST petition to port 8089 and the original JSON {"flag" : "true"} with an adequate set of both Content-type and Accept.

When I play the Main class through the eclipse server correctly, but when I do it in the CLI (mvn clean package + java -cpJar main_class), the server responds with 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 on what's going on?

EDIT: I narrowed the issue a bit. It looks like com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider does not register when the application runs from the console . How can I manually register it?

+5
source share
2 answers

I'm not quite sure what the difference is between starting from the IDE and a jar from the command line, but I was able to reproduce the same problem. I tried using your code above as well as 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 process it was with my sample code, but explicitly registered JacksonFeature

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

I even tried installing init-param to scan the Jackson package, which didn't work either.

+2
source

I had the same problem while running jersey service services in uber jar. I was able to fix this using the following article. Hope this can help someone. JSON example with Jersey + Jackson

The above example was written in jersey 1.x.Here is an implementation of jersey 2.x.

Add the following jersey jackson addiction to your pom

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

And add the following init-param to 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 a jackson function like the one mentioned by @peeskillet.

 ResourceConfig config = new ResourceConfig(); config.register(JacksonFeature.class); 
+2
source

Source: https://habr.com/ru/post/1203733/


All Articles