Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't enable POJO based JSON binding support for Jackson in Jersey 2.0

I'm using Jersey to produce JSON (with POJO mapping through Jackson) and Jetty (start from main method).
It works perfect for Jersey 1.x.:

ServletHolder sh = new ServletHolder(ServletContainer.class);
sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
                        "com.sun.jersey.api.core.PackagesResourceConfig");
sh.setInitParameter("com.sun.jersey.config.property.packages", "service");
sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
...
context.addServlet(sh, "/rest/*");
Server server = new Server(8080);
server.setHandler(context);
server.start();

Now I migrated my project to Jersey 2.0 and failed to enable POJO based JSON binding in it, I get the following: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class business.entity.ExampleEntity, genericType=class business.entity.ExampleEntity.

Obviously, com.sun.jersey.api.json.POJOMappingFeature no longer valid as Jersey goes to org.glassfish

The documentation say the following:

In order to use Jackson as your JSON (JAXB/POJO) provider you need to register JacksonFeature and a ContextResolver for ObjectMapper (if needed) in your Configurable (client/server).

But I can't figure out how to do it correctly in my case.

I created a little project for this question:

branch master - worked example for Jersey 1.17.1;

branch jersey-2.0-migration - not working attempt to migrate to Jersey 2.0 - test failed;

branch jersey-2.0-migrate-client-only - non working attempt to use Jersey client 2.0 with working Jersey server 1.17.1 - test failed.

Question is: how to enable POJO based JSON binding in Jersey 2.0

like image 816
Sergey Petrunin Avatar asked Jul 07 '13 10:07

Sergey Petrunin


2 Answers

The documentation is a bit outdated. The latest Jackson build provides an auto-discoverable provider. Add the following jars to the class path:

1) jackson-annotations-2.2.2.jar

2) jackson-core-2.2.2.jar

3) jackson-databind-2.2.2.jar

4) jackson-jaxrs-base-2.2.1.jar

5) jackson-jaxrs-json-provider-2.2.1.jar

6) jackson-module-jaxb-annotations-2.2.2.jar

Make sure to add "com.fasterxml.jackson.jaxrs.json" to the "jersey.config.server.provider.packages" servlet config property, so the Jackson json provider can be auto-discovered.

like image 177
user2562639 Avatar answered Sep 22 '22 12:09

user2562639


I personally liked @jontro's comment/answer ... so I'm going to re-post it as an answer rather than a comment so that people don't miss it (hopefully that is ok).

Have a look at https://github.com/FasterXML/jackson-jaxrs-providers where there are new jackson jaxrs providers (from the jackson project rather than the jersey project).

Note that this brings in Jackson2 dependencies (jackson-core-2.2.3.jar etc) rather than Jackson1 dependencies that jersey-media-json-jackson brings in (jackson-core-asl-1.9.13.jar etc).

For my maven project using jersey 2.5 this translates into:

Remove the dependency:

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

Add the dependency:

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>2.2.3</version>
</dependency>

Thanks to @user2562639 and @jontro.

like image 35
Rob Bygrave Avatar answered Sep 22 '22 12:09

Rob Bygrave