Spring post method “Required request body is missing”

耗尽温柔 提交于 2020-01-06 05:33:04

问题


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

I have this method for the login in the UserController. The problem is when i try to make the post request for the login i get this error:

{
"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"
}


回答1:


You have to pass that as JSON in your body, if it's a POST request.




回答2:


This is happening because you are not passing a body to you server. As can I see in your screenshot you are passing email and password as a ResquestParam.

To handle this values, you can do the following:

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

In order to accept an empty body you can use the required param in the RequestBody annotation:

@RequestBody(required = false)

But this will not solve your problem. Receiving as RequestParam will.

If you want to use RequestBody you should pass the email and password in the body.




回答3:


You need to send data in Body as JSON

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


来源:https://stackoverflow.com/questions/52974330/spring-post-method-required-request-body-is-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!