Java 缺少所需的请求正文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34533843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:45:03  来源:igfitidea点击:

Required request body is missing

javaspringrestful-url

提问by Damon

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

我是 Spring and Rest 的新手。我写了一个简单的休息是这样的:

@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:

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

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:

而且还在 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)

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

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

回答by 11thdimension

Request body should be sent in --dataswitch, in curl.

请求正文应在 curl中的--data开关中发送。

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

请参阅此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

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