Damon
Damon

Reputation: 41

Required request body is missing

I am new at Spring and Rest. I wrote a simple rest like this:

@RequestMapping(value = "/loginTest", method = RequestMethod.POST)
@ResponseBody
public Response loginTest(@RequestBody LoginRequest request) {
    System.out.println("enter loginTest.");
    String account = request.getAccount();
    String password = request.getPassword();
    Response res = new Response();
    return res;
}

And the LoginRequest is like this:

public class LoginRequest {
    private String account;
    private String password;
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

When I test this via command:

curl -X POST "{"account": "aaa","password": "bbb"}" -H "Content-type:application/json" http://localhost:8080/user/loginTest

But I got the result:

[1/2]: account: aaa --> <stdout>
--_curl_--account: aaa
curl: (6) Could not resolve host: account; nodename nor servname provided, or not known
{
  "timestamp" : "2015-12-30T16:24:14.282+0000",
  "status" : 400,
  "error" : "Bad Request",
  "exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
  "message" : "Bad Request",
  "path" : "/user/loginTest"
}

And also in eclipse console:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.test.response.Response com.test.service.UserService.loginTest(com.test.model.request.LoginResquest)

Does the class LoginRequest need an annotation? Because the Jason cannot be converted to a class? Would anyone help me figure this out?

Upvotes: 4

Views: 46615

Answers (1)

11thdimension
11thdimension

Reputation: 10633

Request body should be sent in --data switch, in curl.

See this https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request

So your request should now become

curl -X POST --data '{"account": "aaa","password": "bbb"}' -H "Content-Type:application/json"  http://localhost:8080/user/loginTest

Also if you can run a browser on the machine where you're sending the requests from, then you can try some REST client plugins. They're way easier to use and provide saving requests and history features. Check this plugin

Upvotes: 7

Related Questions