2017-06-04 216 views
2

如何集成node.js和java微服务。我可以从java spring mvc调用节点休息api,但无法将数据发布到节点api。在节点项目中获取请求对象中的空主体。从Java Spring MVC调用Node.Js Rest API(POST)

FYI:Node.js的(表达)-REST API(POST方法) Spring MVC的-REST -API(POST方法)

的Java:

String url = "localhost:3010/upload"; 
JSONObject jsonob1=new JSONObject(); jsonob1.put("upfile", file); 
jsonob1.put("ty", "ACT"); 
HashMap<String, String> header = getHeaderValues(uploadreq.getAuthToken(), "action"); 
status = getHttpClient.executePostRequest(url,jsonob1,header); 
LOGGER.debug("result {}", status); 

的NodeJS:

var express = require("express"); 
var router = express.Router(); 
router.post('/upload', function (req, res) { console.log(req); }); 
+0

请发布您的代码正在工作,您的代码不是。 –

+0

String url =“http:// localhost:3010/upload”; JSONObject jsonob1 = new JSONObject(); jsonob1.put(“upfile”,file); \t jsonob1.put(“ty”,“ACT”); \t HashMap header = getHeaderValues(uploadreq.getAuthToken(),“action”); \t status = getHttpClient.executePostRequest(url,jsonob1,header); \t LOGGER.debug(“result {}”,status); –

+0

NodeJs- var express = require(“express”); var router = express.Router(); router.post('/ upload',function(req,res){ \t console.log(req); }); –

回答

0

请忽略它,由我自己解决,在nodejs端访问req.body如下

var qs = require('querystring');if(req.method=='POST') { 
    var body=''; 
    req.on('data', function (data) { 
     body +=data; 
    }); 
    req.on('end',function(){ 
     var POST = qs.parse(body); 
     console.log(POST);`` 
    });}