繁体   English   中英

缺少必需的请求正文

[英]Required request body is missing

我是春天和休息的新人。 我写了这样一个简单的休息:

@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;
}

LoginRequest是这样的:

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;
    }
}

当我通过命令测试时:

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

但我得到了结果:

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

而且在eclipse控制台中:

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)

LoginRequest类是否需要注释? 因为杰森不能转为一类? 有人会帮我解决这个问题吗?

请求体应该在--data开关中发送,卷曲。

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

所以你的要求现在应该成了

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

此外,如果您可以在发送请求的计算机上运行浏览器,那么您可以尝试一些REST客户端插件。 它们更易于使用,并提供保存请求和历史记录功能。 检查这个插件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM