繁体   English   中英

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

[英]How to send data to spring boot method using FETCH API

我正在探索fetch api,并希望将一些日期信息发送到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;
        })

弹簧启动方法

@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 presentfetchConfig具有必需的密钥

您的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只是一个具有passworduserName作为属性的bean。

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

暂无
暂无

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

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