POST to Jersey REST Service Encounters 415 Error: Unsupported Media Type
Consider the situation where you encounter a HTTP 415 error when attempting to POST JSON data to a Jersey-powered REST service. This issue may arise in a standard setup involving a HelloWorld.java resource with GET and POST annotations and a request formatted in Postman with an 'application/json' header.
Resolving the Error
The error indicates that the Jersey distribution does not inherently support JSON/POJO conversions. To resolve this, additional dependencies must be introduced to enable JSON processing. These include:
Implementing the Solution with Maven
Using Maven, the following dependency will incorporate all the necessary dependencies:
<dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.17</version> </dependency>
Adapting the Solution to Other Jersey Versions
For users utilizing Jersey versions other than 2.17, refer to the Jersey documentation to identify the required transitive dependency versions. Focus particularly on the Jackson version, as the dependency listed here employs Jackson 2.3.2, ensuring compatibility with this specific version.
The above is the detailed content of Why am I getting a 415 Unsupported Media Type Error When POSTing JSON to a Jersey REST Service?. For more information, please follow other related articles on the PHP Chinese website!