Java HTTP 状态 415 - 请求实体的格式不受支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33457654/
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
HTTP Status 415 - request entity is in a format not supported
提问by nope
I am working on java restful web service. I got it working for GET request, but POST request does not work. My Controller class is RestController
. I have done these so far:
我正在开发 java restful web 服务。我让它为 GET 请求工作,但 POST 请求不起作用。我的控制器类是RestController
. 到目前为止,我已经完成了这些:
@RequestMapping(value = "/api/signup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public long signUp(@ModelAttribute ApiMemberModel apiMember) {
memberService = new MemberDetailsService();
Member m = memberService.createMember(apiMember.getUsername(), apiMember.getPassword(), apiMember.getEmail(), "");
return m.getId();
}
Also tried RequestBody instead of ModelAttribute.
还尝试了 RequestBody 而不是 ModelAttribute。
I use Postman extensionfor sending POST request. For example:
我使用Postman 扩展来发送 POST 请求。例如:
{
"username": "asd",
"password": "sfsdg",
"email": "[email protected]"
}
But I get the error:
但我收到错误:
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
What am I doing wrong? Model class is:
我究竟做错了什么?模型类是:
public class ApiMemberModel {
private String username;
private String password;
private String email;
public ApiMemberModel(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
采纳答案by vtortola
I bet that call from Postman does not include Content-Type: application/json
.
我敢打赌,来自 Postman 的电话不包括Content-Type: application/json
.
HTTP 415 means that the server does not understand the media format of the request. In your controller you are specifying it accepts JSON, but you have not said if the request indicated that the body is in that format. Just because you put the data in JSON format, does not mean that the server is going to recognize it, you have to indicate it in the Content-Type
header.
HTTP 415 表示服务器不理解请求的媒体格式。在您的控制器中,您指定它接受 JSON,但您没有说明请求是否表明正文采用该格式。仅仅因为您将数据放入 JSON 格式,并不意味着服务器会识别它,您必须在Content-Type
标头中指明它。
回答by Pradeep
Spring provides out-of-box many default HttpMessageConverters, which will be used for conversion, depending on presence of certain library in project classpath.
Spring 提供了开箱即用的许多默认 HttpMessageConverters,它们将用于转换,具体取决于项目类路径中某些库的存在。
For example, if the Content-Type in request Header was one of application/json or application/xml , that means the POST body contains json or XML[Popular formats], and if Hymanson library is found in your classpath, Spring will delegate the conversion to MappingHymanson2HttpMessageConverter [for json] or MappingHymanson2XmlHttpMessageConverter [for xml].
例如,如果请求 Header 中的 Content-Type 是 application/json 或 application/xml 之一,则意味着 POST 正文包含 json 或 XML[流行格式],并且如果在您的类路径中找到 Hymanson 库,则 Spring 将委托转换为 MappingHymanson2HttpMessageConverter [for json] 或 MappingHymanson2XmlHttpMessageConverter [for xml]。
To declare a dependency to Hymanson library (Hymanson-databind) include following dependency in your pom.xml
要声明对 Hymanson 库 (Hymanson-databind) 的依赖,请在 pom.xml 中包含以下依赖项
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-databind</artifactId>
<version>${Hymanson.version}</version>
</dependency>
回答by ABHIJITH GM
I too had the same problem. Add ContentType header to application/xml (or whatever ur service is expecting) to the postman.
我也有同样的问题。将 ContentType 标头添加到 application/xml(或您的服务期望的任何内容)给邮递员。
回答by Aleksandar
I'm a bit shamed to write this, but I forgot to put the enctype="multipart/form-data"
attribute in the form tag:
写到这里有点惭愧,但是忘记把enctype="multipart/form-data"
属性放在form标签里了:
<form action="rest/upload_function_path" method="post" enctype="multipart/form-data">
<!-- this thing right --------------------------------^ there! -->
<p>
Your file is: <input type="file" name="fajl" />
</p>
<input type="submit" value="Upload It" />
</form>
This is a pretty lame error, but also the one which took some time to realize.
这是一个相当蹩脚的错误,但也是一个花了一些时间才意识到的错误。
回答by BHARATHWAJ
add "annotation-driven" line to your xml file or add content-type as application/json
将“注释驱动”行添加到您的 xml 文件或将内容类型添加为 application/json