Java 尝试发出发布请求时,Spring Boot“缺少 HttpMessageNotReadableException 必需的请求正文”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43805709/
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
Spring Boot "HttpMessageNotReadableException Required request body is missing" when trying to make a post request
提问by Lightning Jimmy Joe Johnson
I have a controller class with a few methods one of which is a method that is supposed to take POST requests and create a new Account with the JSON from the body of that POST request.
我有一个带有几个方法的控制器类,其中一个方法是应该接受 POST 请求并使用来自该 POST 请求正文的 JSON 创建一个新帐户的方法。
When I try to use curl to make the POST request I get an error that says
当我尝试使用 curl 发出 POST 请求时,我收到一个错误消息
{"timestamp":1493988808871,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: org.springframework.http.ResponseEntity<?> com.example.AccountRestController.add(java.lang.String,java.lang.String)","path":"/users/add"}
The curl command I'm using
我正在使用的 curl 命令
curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/users/add
AccountRestController
帐户休息控制器
@RequestMapping(method = RequestMethod.POST, value = "/add", produces = { MediaType.APPLICATION_JSON_VALUE})
ResponseEntity<?> add(@RequestBody String username, @RequestBody String password) {
Account result = accountRepository.save(new Account (username, password));
return new ResponseEntity<>(result, HttpStatus.CREATED);
}
采纳答案by andreim
You cannot use multiple @RequestBody
. You need to wrap everything into a class that will be used to match your request body.
您不能使用多个@RequestBody
. 您需要将所有内容包装到一个用于匹配您的请求正文的类中。
The same is answered also here.
这里也有同样的回答。
There is also a JIRA issuefor a feature request which was rejected.
对于被拒绝的功能请求,还有一个JIRA 问题。
NOTE:if you want to write less, you can use @PostMapping
instead of @RequestMapping(method = RequestMethod.POST)
.
注意:如果你想写得更少,你可以使用@PostMapping
代替@RequestMapping(method = RequestMethod.POST)
.
NOTE:@RequestParam
and @PathVariable
are used to extract data from URI, not from body.
注意:@RequestParam
and@PathVariable
用于从 URI 中提取数据,而不是从正文中提取数据。
NOTE:the same is valid also for the equivalent [FromBody]
attribute from ASP.NET WebAPI
.
注意:这同样适用于[FromBody]
来自的等效属性ASP.NET WebAPI
。
Complete example:
完整示例:
Bellow I created a working example similar to your case:
波纹管我创建了一个类似于您的案例的工作示例:
Request DTO
请求 DTO
public class AccountCreateRequest {
private String userName;
private String password;
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;
}
}
Response DTO
响应 DTO
public class AccountCreateResponse {
private String userName;
private String password;
public AccountCreateResponse() {
}
public AccountCreateResponse(String userName, String password) {
this.userName = userName;
this.password = password;
}
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;
}
}
Controller
控制器
@RestController
@RequestMapping("/v1/account")
public class AccountController {
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseStatus(HttpStatus.CREATED) AccountCreateResponse add(@RequestBody() AccountCreateRequest account) {
AccountCreateResponse response = new AccountCreateResponse(account.getUserName(), account.getPassword());
return response;
}
}
curl request
卷曲请求
curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/v1/account
回答by Lightning Jimmy Joe Johnson
So I changed @Requestbodys to a single @RequestBodylike andreim said, it fixed my bad request error but for some reason the account username would always be nullwhen the post request went through.
因此,我将@Requestbody更改为单个@RequestBody就像 andreim 所说的那样,它修复了我的错误请求错误,但由于某种原因,当发布请求通过时,帐户用户名始终为空。
I messed with a few thing, eventually leading me to swap the order of the Account constructor parameters from
我搞砸了一些事情,最终导致我从以下位置交换 Account 构造函数参数的顺序
Account(String username, String password)
to
到
Account(String password, String username)
This worked for some reason and let me make my post request with the proper values. Latter out of curiosity I decided to swap the parameters back to Account(String username, String password)
and the post request still works as intended.
这出于某种原因起作用,让我用正确的值发出我的帖子请求。后来出于好奇,我决定将参数交换回Account(String username, String password)
并且 post 请求仍然按预期工作。
I have no idea what I did to get rid of the nullproblem but it seams to have worked.
我不知道我做了什么来摆脱空问题,但它似乎奏效了。