Java REST Web 服务返回 415 - 不支持的媒体类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24932080/
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
REST Webservice returning 415 - Unsupported Media Type
提问by
I've created a REST webservice using jax-rs and jersey that is supposed to consume JSON on a POST request. My web service class looks like this:
我已经使用 jax-rs 和 jersey 创建了一个 REST web 服务,它应该在 POST 请求上使用 JSON。我的 Web 服务类如下所示:
@Path("/webhookservice")
public class Webhook {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response readData (Song song) {
// Prints out the song info
System.out.println("SONG INFO \n=======================");
System.out.println("songname: " + song.getSongname());
System.out.println("artist: " + song.getArtist());
// Repsonse with a HTTP 200 OK
Response response = Response.status(200).build();
return response;
}
}
My Song class:
我的歌课:
public class Song {
private String songname;
private String artist;
public String getSongname () { return this.songname; }
public String getArtist () { return this.artist; }
public void setSongname (String songname) { this.songname = songname; }
public void setArtist (String artist) { this.artist = artist; }
}
My web.xml (if needed)
我的 web.xml(如果需要)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>SnapScan-Webhook</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>za.co.lancet.service</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SnapScan-Webhook</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
I'm using RESTClienta little, well, rest client... Here's a screenshot of what I'm sending:
我正在使用RESTClient一点,好吧,休息客户端......这是我发送的截图:
When I send that off, I get the 415 Unsupported Media Typeerror. Anybody have an idea why?
当我发送它时,我收到415 Unsupported Media Type错误。有人知道为什么吗?
采纳答案by lefloh
You need to send the request-header Content-Type: application/json
. Seems like REST-Client does not add this header automatically for you.
您需要发送 request-header Content-Type: application/json
。似乎 REST-Client 不会自动为您添加此标头。
回答by user3811473
It might be because you've not specified a path for the API function. You have only specified the resource path in your code.
可能是因为您没有为 API 函数指定路径。您仅在代码中指定了资源路径。
@POST
@Path("/somepath")
@Consumes(MediaType.APPLICATION_JSON)
public Response readData (Song song) {
...
}
回答by Iain T
I tend to add
我倾向于添加
@Produces({"application/json"})
to my services against the top-level Class declaration and override where appropriate.
我的服务针对顶级 Class 声明并在适当的情况下覆盖。
e.g.
例如
@Path("/foo")
@Produces({"application/json"})
public class FooRestService {
回答by grepit
As others have pointed out, you are missing the correct header. Add Content-Type: application/json
to the "Headers":
正如其他人指出的那样,您缺少正确的标题。添加Content-Type: application/json
到“标题”:
回答by Fabiano Tarlao
I had the same 415
http error time ago. I simply forgot the default no-parameters constructor in my DTO classes. Adding that constructor, in a similar way as for JPA entities, solved my issue and de-serialization JSON->Object
works now.
我之前也遇到过同样的415
http 错误。我只是忘记了 DTO 类中默认的无参数构造函数。以与 JPA 实体类似的方式添加该构造函数,解决了我的问题并且反序列化JSON->Object
现在可以工作了。
I'm not sure this is your case, looking at your code, but it could be useful to other guys falling here looking at the 415+JSON issue. Regards
我不确定这是你的情况,看看你的代码,但它可能对其他在这里查看 415+JSON 问题的人有用。问候
回答by Kapil Reddy
Ok so there are error codes that commonly appear during content negotiation 1) 406 - Not acceptable 2) 415 - Unsupported Media Type
好的,所以在内容协商期间通常会出现错误代码 1) 406 - 不可接受 2) 415 - 不支持的媒体类型
406 is when the server doesn't accept the content type that is being sent under the ACCEPT header of the request.
406 表示服务器不接受在请求的 ACCEPT 标头下发送的内容类型。
415 is the client is sending a content-type in the request header and so server straightforwardly rejects saying unsupported media type
415 是客户端在请求头中发送内容类型,因此服务器直接拒绝说不支持的媒体类型
to overcome 406 - we must include the appropriate dependent jars say a client wants an XML input to be understood by the server, then the server needs to have XML related dependencies.
为了克服 406 - 我们必须包含适当的依赖 jar,比如客户端希望服务器理解 XML 输入,然后服务器需要具有与 XML 相关的依赖项。
to overcome 415 - understand the media types that are supported by the server and pass the correct media type in the content-type header of the request
克服 415 - 了解服务器支持的媒体类型并在请求的 content-type 标头中传递正确的媒体类型
回答by Hasnaa Ibraheem
Although it could be silly experience to share, I'm going to share it in case it saves the day of somebody else.
I had set of consecutive functions, the first was with annotation @BodyParser.Of(BodyParser.Json.class)
, when I commented that first function the annotation line was left uncommented by mistake, so such annotation was applied on that second function resulting in such 415 media type error
hth
虽然分享可能是愚蠢的经验,但我会分享它,以防它挽救其他人的一天。
我有一组连续的函数,第一个是带有注释的@BodyParser.Of(BodyParser.Json.class)
,当我评论第一个函数时,注释行被错误地未注释,因此将此类注释应用于第二个函数,导致 415 媒体类型错误
hth