将 @RequestBody 从节点 js 发送到 Spring Boot Rest API

Sending @RequestBody from node js to Spring Boot Rest API

折腾了大半天还是没能搞清楚下面的问题:

正在尝试从 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();

Spring 休息:

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

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

使用邮递员,我能够看到相同的输入数据通过其余部分。但是当从 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。

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

我认为您将请求正文放在 queryParams 中是行不通的。
您可以尝试使用 req.write() 向请求体写入数据,如下所示:

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