[英]How to send data to spring boot method using FETCH API
I am exploring fetch api and want to send some for date to a spring boot method, but it is throwing 400
error 我正在探索fetch api,并希望将一些日期信息发送到spring boot方法,但是会引发
400
错误
JS code JS代码
let fetchConfig = {
method : params.method || 'GET',
body : params.body
};
return fetch(_url,fetchConfig).then(function (response) {
if (!response.ok) {
throw Error(response.statusText);
}
else {
return response.json();
}
}).then(function (data) {
return data;
}).catch(function (error) {
return error;
})
Spring boot method 弹簧启动方法
@RequestMapping(value = "/validate",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ValidateUserModel validateUser(@RequestParam("userName") String userName, @RequestParam("password") String pass){
System.out.println("--- test");
ValidateUserModel validateUserModel = new ValidateUserModel();
validateUserModel.setUserName("Test");
validateUserModel.setPassWord("Test 2");
return validateUserModel;
}
This is how fetchConfig
looks like 这就是
fetchConfig
样子
body:{userName: "awdaw", password: "awdawd"}
method:"POST"
password:"test"
url:"test"
userName:"awdaw"
__proto__:Object
Preview of the values send from java class 从Java类发送的值的预览
error:"Bad Request"
exception:"org.springframework.web.bind.MissingServletRequestParameterException"
message:"Required String parameter 'userName' is not present"
path:"/validate"
status:400
timestamp:1504771336175
The error says Required String parameter 'userName' is not present
but the fetchConfig
have the required key 错误显示
Required String parameter 'userName' is not present
但fetchConfig
具有必需的密钥
Your Java code expects request params, so the expected url should be something like .../validate?username=<someValue>&password=<otherValue>
, but your js is sending that in the body (which is a better option, sending user and password as part of the URL is not secure at all). 您的Java代码期望请求参数,因此期望的URL应该类似于
.../validate?username=<someValue>&password=<otherValue>
,但是您的js会将其发送到正文中(这是一个更好的选择,发送用户和作为URL一部分的密码根本不安全)。
You could change your Java code to something like 您可以将Java代码更改为
@RequestMapping(value = "/validate",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ValidateUserModel validateUser(@RequestBody User user){
System.out.println("--- test");
ValidateUserModel validateUserModel = new ValidateUserModel();
validateUserModel.setUserName(user.getUserName());
validateUserModel.setPassWord(user.getPassword());
return validateUserModel;
}
Where User
is just a bean with password
and userName
as attributes. 其中
User
只是一个具有password
和userName
作为属性的bean。
Try Using @RequestBody annotation. 尝试使用@RequestBody批注。
@RequestMapping(value = "/validate",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ValidateUserModel validateUser(@RequestBody User user){
System.out.println("--- test");
ValidateUserModel validateUserModel = new ValidateUserModel();
validateUserModel.setUserName(user.getUserName());
validateUserModel.setPassWord(user.getPassword());
return validateUserModel;
}
and the request should look like: 该请求应如下所示:
let data = {userName: "awdaw", password: "awdawd"};
fetch{ url, {
body: JSON.stringfy(data)
method:"POST"
headers: {
"Content-Type": "application/json",
},
}).then(
response => {
return response.json();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.