gpt4 book ai didi

java - "HttpMessageNotReadableException Required request body is missing"尝试发出发布请求时

转载 作者:行者123 更新时间:2023-11-30 06:06:34 28 4
gpt4 key购买 nike

我有一个包含几个方法的 Controller 类,其中一个方法应该接受 POST 请求并使用来自该 POST 请求正文的 JSON 创建一个新帐户。

当我尝试使用 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"}

我正在使用的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);
}

最佳答案

您不能使用多个@RequestBody。您需要将所有内容包装到一个类中,该类将用于匹配您的请求正文。

同样的回答也是here .

还有一个JIRA issue对于被拒绝的功能请求。

注意:如果想少写一些,可以使用@PostMapping代替@RequestMapping(method = RequestMethod.POST)

注意 @RequestParam@PathVariable 用于从 URI 中提取数据,而不是从正文中提取数据。

注意:这同样适用于 ASP.NET WebAPI 中的等效 [FromBody] 属性。

完整示例:

下面我创建了一个类似于您的案例的工作示例:

请求 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;
}
}

响应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 请求

curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/v1/account

关于java - "HttpMessageNotReadableException Required request body is missing"尝试发出发布请求时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43805709/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com