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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With