POST请求方式1.使用实体类接收consthttp=require('http');constpostData=JSON.stringify({"id":1,"name":"三体","price":180});constoptions={hostname:'localhost',port:8080,path:'/ReceiveJsonController/receiveJson1',method:'POST',headers:{'Content-Type':'application/json','Content-Length':Buffer.byteLength(postData)}}constreq=http.request(options,res=>{console.log(`statuscode:${res.statusCode}`)res.on('data',d=>{process.stdout.write(d)})})req.on('error',error=>{console.error(error)})req.write(postData)req.end()@PostMapping("/receiveJson1")publicvoidreceiveJson1(@RequestBodyBookbook,HttpServletRequesthttpServletRequest){logger.info("请求方法:"+httpServletRequest.getMethod());logger.info("Data:"+book);}2.使用List实体类接收consthttp=require('http');constpostData=JSON.stringify([{"id":1,"name":"三体","价格":180},{"id":1,"名称":"三体","价格":180},{"id":1,"名称":"三体》"price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","价格":180}]);constoptions={hostname:'localhost',port:8080,path:'/ReceiveJsonController/receiveJson2',method:'POST',headers:{'Content-Type':'application/json','Content-Length':Buffer.byteLength(postData)}}constreq=http.request(options,res=>{console.log(`status:${res.statusCode}`)res.on('data',d=>{process.stdout.write(d)})})req.on('error',error=>{console.error(error)})req.write(postData)req.end()@PostMapping("/receiveJson2")publicvoidreceiveJson2(@RequestBodyListbooks,HttpServletRequesthttpServletRequest){logger.info("请求方法:"+httpServletRequest.getMethod());logger.info("Data:"+books);}3.使用Map接收consthttp=require('http');constpostData=JSON.stringify({"data":{"id":1,"name":"三体","价格":180}});constoptions={hostname:'localhost',port:8080,path:'/ReceiveJsonController/receiveJson3',method:'POST',headers:{'Content-Type':'application/json','Content-Length':Buffer.byteLength(postData)}}constreq=http.request(options,res=>{console.log(`statuscode:${res.statusCode}`)res.on('data',d=>{process.stdout.write(d)})})req.on('error',error=>{console.error(error)})req.write(postData)req.end()@PostMapping("/receiveJson3")publicvoidreceiveJson3(@RequestBodyMapparamsMap,HttpServletRequesthttpServletRequest){logger.info("请求方法:"+httpServletRequest.getMethod());logger.info("Data:"+paramsMap);}使用Map接收,注意Key:Value的形式//singleobject{"data":{"id":1,"name":"三体","price":180}}//多个对象{"data":[{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"三体","price":180},{"id":1,"name":"Three-body","price":180}]}get请求方法get请求方法需要注意encodeURIComponent()转义。转义后数据是一串字符串,只能用String接收,然后用Java的json工具类转换成对应的Objectconsthttp=require('http')constoptions={hostname:'localhost',端口:8080,路径:'/ReceiveJsonController/receiveJson5',method:'GET'}letdata={"id":1,"name":"三体","price":180};letjsonString="?data="+encodeURIComponent(JSON.stringify(data));options.path=options.path+jsonStringconstreq=http.request(options,res=>{console.log(`statuscode:${res.statusCode}`)res.on('data',d=>{process.stdout.write(d)})})req.on('error',error=>{console.error(error)})req.end()@GetMapping("/receiveJson5")publicvoidreceiveJson5(@RequestParam("data")Stringdata,HttpServletRequesthttpServletRequest){logger.info("请求方法:"+httpServletRequest.getMethod());logger.info("Data:"+data);}使用总结在post请求中传json还是最好的,用get也不是不行,只是get有长度限制。