gpt4 book ai didi

java - 缺少必需的请求正文

转载 作者:行者123 更新时间:2023-12-02 07:24:45 24 4
gpt4 key购买 nike

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

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 是否需要注解?因为 Jason 无法转换为类?谁能帮我解决这个问题?

最佳答案

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

查看此 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 客户端插件。它们更易于使用并提供保存请求和历史记录功能。检查this plugin

关于java - 缺少必需的请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34533843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com