Java POST 到 Jersey REST 服务获取错误 415 不支持的媒体类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30423776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
POST to Jersey REST service getting error 415 Unsupported Media Type
提问by Rob Crocombe
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.
我正在使用带有 Jersey 和 Tomcat 的 JAX-RS Web 应用程序。获取请求很好,但是当我尝试发布 JSON 时,我得到一个 HTTP 状态 415 - 不支持的媒体类型。
Here is my simple HelloWorld.java:
这是我的简单 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;
}
}
Here is the request I try in Postman (with 'application/json' header):
这是我在 Postman 中尝试的请求(带有“application/json”标头):
Here is the project layout with libraries:
这是带有库的项目布局:
I am using:
我在用:
- Java 7 x64
- Jersey 2.17
- Tomcat 7.0.62 x64
- Java 7 x64
- 泽西岛 2.17
- 雄猫 7.0.62 x64
Thanks!
谢谢!
采纳答案by Paul Samsotha
The Jersey distribution doesn't come with JSON/POJO support out the box. You need to add the dependencies/jars.
Jersey 发行版不附带现成的 JSON/POJO 支持。您需要添加依赖项/jar。
Add all these
添加所有这些
- jersey-media-json-Hymanson-2.17
- Hymanson-jaxrs-json-provider-2.3.2
- Hymanson-core-2.3.2
- Hymanson-databind-2.3.2
- Hymanson-annotations-2.3.2
- Hymanson-jaxrs-base-2.3.2
- Hymanson-module-jaxb-annotations-2.3.2
- jersey-entity-filtering-2.17
- jersey-media-json-Hymanson-2.17
- Hymanson-jaxrs-json-provider-2.3.2
- Hyman逊核心2.3.2
- Hyman逊-数据绑定-2.3.2
- Hyman逊注释2.3.2
- Hymanson-jaxrs-base-2.3.2
- Hymanson-module-jaxb-annotations-2.3.2
- 球衣实体过滤2.17
With Maven, below will pull all the above in
使用 Maven,下面将把以上所有内容都拉进来
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-Hymanson</artifactId>
<version>2.17</version>
</dependency>
For any future readers not using Jersey 2.17 (and using jars directly instead of Maven), you can go hereto find the Jersey version you are using, and see what transitive dependency versions you need. The current version of this Jersey dependency uses Hymanson 2.3.2. That's the main thing you need to look out for.
对于未来不使用 Jersey 2.17(并直接使用 jars 而不是 Maven)的读者,您可以到这里找到您正在使用的 Jersey 版本,并查看您需要哪些传递依赖版本。此 Jersey 依赖项的当前版本使用 Hymanson 2.3.2。这是你需要注意的主要事情。
回答by Deniz
I think you post request is working correctly but your browser/Postman can not parse the response returned from the service. It is returning plain text and Postman is waiting json. Add Produce annotation to your web service and set it to json too. I also suggest you to check with Fiddler2 to see exact information going over the wire.
我认为您发布的请求工作正常,但您的浏览器/邮递员无法解析从服务返回的响应。它返回纯文本,邮递员正在等待 json。将 Produce 注释添加到您的 Web 服务并将其设置为 json。我还建议您与 Fiddler2 核对以查看通过网络传输的确切信息。
回答by Suresh Patil
Check your REST call contentType. It should be contentType: 'application/json', if you are passing json data to POJO class.
检查您的 REST 调用 contentType。如果您将 json 数据传递给 POJO 类,它应该是 contentType: 'application/json'。
回答by Young Emil
Gone through a lot of the answers both on this page and others but to no avail. This actually worked for me:
在此页面和其他页面上浏览了很多答案,但无济于事。这实际上对我有用:
METHOD 1:Instead of passing JSONObject
as parameter to the resource method, pass a String
rather. Take the String
and create an JSONObject
with it
and then you can use it in you code. Like so,
方法 1:不是JSONObject
作为参数传递给资源方法,String
而是传递一个。获取String
并JSONObject
使用它创建一个,然后您可以在代码中使用它。像这样,
@Path("/people")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response doGetperson(String jsonRequest) {
try {
JSONObject requestedJSON = new JSONObject(jsonRequest);
//So now you can use requestedJSON object created to do your stuff
return Response.ok("{\"name\":" + requestedJSON.getString("user") + "}").build();
} catch (Exception ex) {
return Response.ok("{ \"name\":\"\"}").build();
}
}
METHOD 2:
方法二:
Adding this dependency as of September, 2017:
自2017 年 9 月起添加此依赖项:
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.26</version>
</dependency>
回答by Deepali Maniyar
I was using firebase APIs which was internally using gson (JSON) apis and jersey JSON databinding wanted jersey-media-json-Hymanson-2.29.jar. Default class path was keeping firebase gson APIs as first JSON APIs and hence things were not working.
我使用的是内部使用 gson (JSON) apis 和 jersey JSON 数据绑定想要 jersey-media-json-Hymanson-2.29.jar 的 firebase API。默认类路径将 firebase gson API 保留为第一个 JSON API,因此事情无法正常工作。
Changed the manifest.mf for classpath sequence and things started working for me. Although I couldn't continue with maven in this scenarios and couldn't figure out as how to add our custom classpath in Maven built war's Manifest.mf.
更改了类路径序列的 manifest.mf,事情开始对我有用。尽管在这种情况下我无法继续使用 Maven,并且无法弄清楚如何在 Maven 构建的战争的 Manifest.mf 中添加我们的自定义类路径。
Added below class path entry in Manifest.mf in of dynamic web project. Copied source of my project to source and downloaded jars of maven to web-inf\lib. Created war using eclipse export as WAR file and things worked for me.
在动态 Web 项目的 Manifest.mf 中添加了以下类路径条目。将我的项目的源复制到源并将 Maven 的 jars 下载到 web-inf\lib。使用 eclipse 导出作为 WAR 文件创建了战争,事情对我有用。
Class-Path: jersey-media-json-Hymanson-2.29.jar Hymanson-databind-2.9.9.jar gson-2.6.2.jar json-20160212.jar google-http-client-gson-1.21.0.jar
类路径:jersey-media-json-Hymanson-2.29.jar Hymanson-databind-2.9.9.jar gson-2.6.2.jar json-20160212.jar google-http-client-gson-1.21.0.jar
Most of the miscellaneous errors are due to classpath conflicts when you are using multiple APIs in your project. Gets really difficult to figure out.
大多数杂项错误是由于在项目中使用多个 API 时的类路径冲突。真的很难弄清楚。