花了半天多的时间仍然无法解决以下问题:
尝试将表单数据从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\"}";
}
使用邮递员,我能够看到通过Rest的相同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。
我在这里遗漏了什么?任何建议将不胜感激!
答案 0 :(得分:1)
我认为您在queryParams
中提出请求正文无效
您可以尝试使用req.write()
将数据写入请求正文,如下所示:
...
req.write(inputData);
req.end();
...