【问题标题】:Sending @RequestBody from node js to Spring Boot Rest API将 @RequestBody 从节点 js 发送到 Spring Boot Rest API
【发布时间】:2017-10-02 23:45:50
【问题描述】:

花了半天多后仍然无法理解以下问题:

尝试从 NodeJS 向 Spring Rest API 发送表单数据。

节点 JS:

var inputData = { base : req.body.base, test : req.body.test }

var queryParams = {
          host: '127.0.0.1',
          port: 8080,
          path: '/start',
          method: 'POST',
          headers: {'Content-type': 'application/json'},
          body: inputData //Used JSON.stringify(inputData) - didn't work
        };

使用http模块发送请求:

var req = http.request(queryParams, function(res) {
        //do something with response
    });
    req.end();

弹簧休息:

    @RequestMapping(value = "/start", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public String startApp(@RequestBody String body) {
        System.out.println(body);

        return "{\"msg\":\"Success\"}";
    }

使用邮递员,我可以看到通过其余部分的相同 inputData。但是当从 NodeJS 发送时,我看到的只是

    { 
      timestamp: 1506987022646,
      status: 400,
      error: 'Bad Request',
      exception: 'org.springframework.http.converter.HttpMessageNotReadableException',
      message: 'Required request body is missing: public java.lang.String ApplicationController.startApp(java.lang.String)',
      path: '/start' 
    }

在 maven 中使用 spring-boot-starter parent。

我在这里遗漏了什么吗?任何建议将不胜感激!

【问题讨论】:

    标签: java node.js spring-boot spring-rest


    【解决方案1】:

    我认为您将请求正文放入 queryParams 不会起作用。
    您可以尝试使用req.write() 将数据写入请求正文,如下所示:

    ...  
    req.write(inputData);  
    req.end();
    ... 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 2017-10-18
      • 2020-03-12
      • 2021-01-24
      • 2016-11-25
      • 2018-10-07
      • 2019-04-17
      相关资源
      最近更新 更多