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


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01