I finally found my answer. I added
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
to my pom.xml file. Then I added
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
to my web.xml file, and everything works fine. No change was required to my code above.
I added following jars and it worked for me
Just a small addition. In case if you do not use the web.xml descriptor file, you may enable the POJOMappingFeatire programatically as shown below
...
final ResourceConfig rc = new PackagesResourceConfig("com.test.resources");
final Map<String, Object> config = new HashMap<String, Object>();
config.put("com.sun.jersey.api.json.POJOMappingFeature", true);
rc.setPropertiesAndFeatures(config);
...
<!-- For Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.15</version>
</dependency>
<!-- For JSON -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.15</version>
</dependency>
XML output is supported by default. So no dependency needed for it.
Working with Jersey 1.8, I was getting the same error while creating a JSON object and hitting a REST API from client side.
If you are facing issue at server side then, make sure that you have correctly included jersey-json.jar in classpath and mapped correct init-param in web.xml as mentioned in other answers.
As per Jersey 1.8 documentation, the following snippet shows how to use the POJO JSON mapping feature on the client side:
ClientConfig clientConfig = new DefaultClientConfig();
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,Boolean.TRUE);
Client client = Client.create(clientConfig);
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