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 Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
