【问题标题】:Required String parameter 'name' is not present必需的字符串参数“名称”不存在
【发布时间】:2019-07-12 18:58:33
【问题描述】:

我收到标题中提到的错误。

不知道怎么回事

在 React Native 中:

var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
      xmlhttp.open("POST", "http://[my ip address]:8000/add");
      xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
      xmlhttp.send(JSON.stringify({name: this.state.name}));

在 Spring Boot 中:

@RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String getFoos(@RequestParam String name) {
        System.out.println("Received POST request:" + name);
        return null;
    }

【问题讨论】:

    标签: spring-boot react-native post


    【解决方案1】:

    前端

    在这里,您将请求作为附加到 URL 的 Path 变量。

    http://[my ip address]:8000/add/stateName

    var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
          xmlhttp.open("POST", "http://[my ip address]:8000/add?name="+ this.state.name);
          xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
          xmlhttp.send();
    

    后端

    @RequestMapping(value = "/add/{name}", method = RequestMethod.POST)
        @ResponseBody
        public String getFoos(@PathVariable(name="name") String name) {
            System.out.println("Received POST request:" + name);
            return name;
        }
    

    注意:如果您使用@RestController,则无需使用@ResponseBody

    如果您想像下面这样发送多个数据,那么理想的方法是在后端创建一个 DAO/DTO

     const booking = { 
       name: this.state.name, 
       location: this.state.location, 
       pax: this.state.pax, 
    };
    

    后台

    public class StateDto{
        private String name;
        private String location;
        private String pax;
        //Getter-Setters, AllArgConstructor-SuperConstructor
    }
    

    那么你的控制器将如下所示

    @RequestMapping(value = "/add", method = RequestMethod.POST)
        @ResponseBody
        public String getFoos(@RequestBody StateDto stateDto) {
            System.out.println("Received POST request:" + stateDto.getName());
            return stateDto.getName();
        }
    

    【讨论】:

      【解决方案2】:

      如果您不想更改前端代码,您可以将后端代码从 @RequestParam 更改为 @RequestBody,因为您不是在前端添加参数而是添加正文。

      【讨论】:

      • 如果我有更多参数怎么办?比如名字:this.state.name,位置:this.state.location,人:this.state.people
      • 等我添加到 this 中,const booking = { name: this.state.name, location: this.state.location, pax: this.state.pax, };
      • xmlhttp.send(JSON.stringify({booking}));
      • 我想单独保存这些参数
      • 这是我从上面得到的结果:
      【解决方案3】:

      客户端在请求正文中传递“名称”,但服务器期望“名称”作为请求参数。

      要将“名称”作为请求参数传递,您可以试试这个:

      var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
            xmlhttp.open("POST", "http://[my ip address]:8000/add?name="+ this.state.name);
            xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            xmlhttp.send();
      

      【讨论】:

      • 这不起作用,我必须在 Spring Boot 方面进行任何更改吗?
      • 使用@RequestParam("name") String name 而不是@RequestParam String name
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 2021-04-01
      • 2021-03-28
      • 2018-05-03
      • 1970-01-01
      • 2017-11-27
      • 2017-11-24
      相关资源
      最近更新 更多