POST to Jersey REST service getting error 415 Unsupported Media Type(POST 到 Jersey REST 服务出现错误 415 Unsupported Media Type)
问题描述
我正在使用带有 Jersey 和 Tomcat 的 JAX-RS Web 应用程序.获取请求很好,但是当我尝试发布 JSON 时,我得到一个 HTTP 状态 415 - 不支持的媒体类型.
I am using a JAX-RS web application with Jersey and Tomcat. Get requests are fine however when I try to post JSON I get an HTTP status 415 - Unsupported Media Type.
这是我的简单 HelloWorld.java:
Here is my simple HelloWorld.java:
package service;
import javax.ws.rs.*;
@Path("hello")
public class HelloWorld {
@GET
@Produces("text/plain")
public String get() {
return "hello world";
}
@POST
@Consumes("application/json")
public String post(JS input) {
return input.hello;
}
public static class JS {
public String hello;
}
}
这是我在 Postman 中尝试的请求(带有 'application/json' 标头):
Here is the request I try in Postman (with 'application/json' header):
这是带有库的项目布局:
Here is the project layout with libraries:
我正在使用:
- Java 7 x64
- 球衣 2.17
- Tomcat 7.0.62 x64
谢谢!
推荐答案
Jersey 发行版不提供开箱即用的 JSON/POJO 支持.您需要添加依赖项/jar.
The Jersey distribution doesn't come with JSON/POJO support out the box. You need to add the dependencies/jars.
添加所有这些
- jersey-media-json-jackson-2.17
- jackson-jaxrs-json-provider-2.3.2
- jackson-core-2.3.2
- jackson-databind-2.3.2
- jackson-annotations-2.3.2
- jackson-jaxrs-base-2.3.2
- jackson-module-jaxb-annotations-2.3.2
- 球衣实体-filtering-2.17
使用 Maven,下面将把上面的所有东西都拉进去
With Maven, below will pull all the above in
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
对于未来不使用 Jersey 2.17(并且直接使用 jars 而不是 Maven)的读者,您可以访问 here 找到您正在使用的 Jersey 版本,并查看您需要哪些传递依赖版本.此 Jersey 依赖项的当前版本使用 Jackson 2.3.2.这是您需要注意的主要问题.
For any future readers not using Jersey 2.17 (and using jars directly instead of Maven), you can go here to find the Jersey version you are using, and see what transitive dependency versions you need. The current version of this Jersey dependency uses Jackson 2.3.2. That's the main thing you need to look out for.
这篇关于POST 到 Jersey REST 服务出现错误 415 Unsupported Media Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:POST 到 Jersey REST 服务出现错误 415 Unsupported Media Type


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01