6

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

Sergey Petrunin
  • 241
  • 3
  • 9

2 Answers2

15

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.

user2562639
  • 206
  • 3
  • 5
  • It worked for me! Big thanks! I added two new branches to my little [project](https://github.com/halfstrik/Jersey20Migration/tree/jersey-2.0-migration-success) on GitHub `jersey-2.0-migrate-server-success` and `jersey-2.0-migration-success` they describe solution and it passed test! – Sergey Petrunin Jul 09 '13 at 19:29
  • I also get to work with `jersey-media-json-jackson 2.0` (see update at `jersey-2.0-migration`), but without `jersey-client 2.0` it gets warnings like mentioned in [comments](http://stackoverflow.com/a/16993761/846325) – Sergey Petrunin Jul 09 '13 at 20:30
  • Hours of anguish gone. Moxy was failing silently and I had to move back to Jackson and I was ready to put my head through a window. Thanks for this answer. – JSager Aug 28 '13 at 17:04
  • Should it also work for jersey 2.1.3 which is the latest stable release? – basilboli Oct 03 '13 at 16:31
  • 2
    Check out https://github.com/FasterXML/jackson-jaxrs-providers for an easy to use maven dependency – jontro Nov 01 '13 at 17:36
  • 1
    When I do this it randomly uses either JacksonJsonProvider or JacksonJaxbJsonProvider. I use JAXB annotations so if it happens to use JacksonJsonProvider, it ignores them and my app breaks. What is the correct way to force the use of JacksonJaxbJsonProvider? – CTarczon Nov 20 '13 at 17:32
  • I struggled for hours until I got it working by reading your post. Thanks! – working Jul 25 '14 at 04:45
3

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.

Rob Bygrave
  • 3,861
  • 28
  • 28