尝试发布请求时,Spring Boot“HttpMessageNotReadableException缺少必需的请求正文”

问题描述 投票:2回答:2

我有一个控制器类,其中包含一些方法,其中一个方法应该接受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);
}
java spring spring-boot
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.