小编Lig*_*son的帖子

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

我有一个带有几个方法的控制器类,其中一个方法应该接收POST请求,并从该POST请求的主体中使用JSON创建一个新Account。

当我尝试使用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"}
Run Code Online (Sandbox Code Playgroud)

我正在使用的curl命令

curl -X POST --data '{"userName":"bepis", "password":"xyz"}' -H "Content-Type:application/json" http://localhost:8080/users/add
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1