Spring post方法“缺少所需的请求正文”

时间:2018-10-24 16:55:40

标签: spring rest spring-boot post

@PostMapping(path="/login")
public ResponseEntity<User> loginUser(@RequestBody Map<String, String> userData) throws Exception {
    return ResponseEntity.ok(userService.login(userData));
}

我在UserController中使用此方法进行登录。问题是,当我尝试发出登录请求时,出现此错误:

{
"timestamp": "2018-10-24T16:47:04.691+0000",
"status": 400,
"error": "Bad Request",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<org.scd.model.User> org.scd.controller.UserController.loginUser(java.util.Map<java.lang.String, java.lang.String>) throws java.lang.Exception",
"path": "/users/login"
}

enter image description here

3 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为您没有将主体传递给服务器。 在您的屏幕截图中可以看到,您正在将电子邮件和密码作为ResquestParam传递。

要处理此值,可以执行以下操作:

@PostMapping(path="/login")
public ResponseEntity<User> loginUser(@RequestParam("email") String email, @RequestParam("password") String password) {
     //your imp
}

要接受一个空的正文,可以在RequestBody批注中使用required参数:

@RequestBody(required = false)

但这不会解决您的问题。接收为RequestParam。

如果要使用RequestBody,则应在正文中传递电子邮件和密码。

答案 1 :(得分:0)

您需要在Body中以JSON格式发送数据

 { "email":"email@email.com", "password":"tuffCookie"}

答案 2 :(得分:0)

如果是POST请求,则必须将其作为JSON传递给您。

enter image description here

相关问题