如何使用FETCH API将数据发送到spring Boot 方法

8ljdwjyq  于 2022-12-04  发布在  Spring
关注(0)|答案(2)|浏览(227)

我正在研究fetch api,并希望将一些for date发送到一个spring Boot 方法,但它抛出了400错误
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 法

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

这是fetchConfig的外观

body:{userName: "awdaw", password: "awdawd"}
method:"POST"
password:"test"
url:"test"
userName:"awdaw"
__proto__:Object

预览从Java类发送的值

error:"Bad Request"
exception:"org.springframework.web.bind.MissingServletRequestParameterException"
message:"Required String parameter 'userName' is not present"
path:"/validate"
status:400
timestamp:1504771336175

错误显示为Required String parameter 'userName' is not present,但fetchConfig具有所需的密钥

jdzmm42g

jdzmm42g1#

您的Java代码需要请求参数,因此所需的url应该类似于.../validate?username=<someValue>&password=<otherValue>,但您的js在正文中发送它(这是一个更好的选择,将用户和密码作为URL的一部分发送根本不安全)。
您可以将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;
}

其中User只是一个Bean,其属性为passworduserName

h5qlskok

h5qlskok2#

请尝试使用@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;
}

请求应如下所示:

let data = {userName: "awdaw", password: "awdawd"};
    fetch{ url, {
      body: JSON.stringfy(data)
      method:"POST"
      headers: {
               "Content-Type": "application/json",
           },
     }).then(
            response =>  {
                return response.json();
            }

相关问题