我有一个带有几个方法的控制器类,其中一个方法应该接收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)