【发布时间】:2017-10-03 23:32:04
【问题描述】:
我有一个控制器类,其中包含一些方法,其中一种方法应该接受 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);
}
【问题讨论】:
-
换成
ResponseEntity<?> add(@RequestParam("userName") String userName, @RequestParam("password") String password)怎么办?
标签: java spring-mvc