Calling Node.Js Rest API(POST) from Java Spring MVC - java

How to integrate node.js and java micro services. I'm able to call node rest api from java spring mvc but not able to post data to node api. Getting empty body in request object at node project.
FYI: Node.js(express) -REST api(POST method)
Spring MVC -REST -api(POST method)
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); });

Please ignore it, its resolved by myself, Access req.body at nodejs side as following
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);``
});}

Related

Unable to send params in servlet

I have angular code
app.controller('add', function ($scope, $http) {
$scope.add = function() {
$scope.msg = "no connect";
var data = {name:'soso',description:'buba',method:'POST'};
$http.post(host + "/add-component",data)
.then(function (response) {
$scope.msg = response.data;
});
}
});
in my servlet, I want to catch it
resp.setContentType("application/json; charset = utf8");
String name = req.getParameter("name");
String description = req.getParameter("description");
but my name and description both = null;
You are sending data as POST request body, not request parameters. This is documented in the angular docs for post method:
url string
Relative or absolute URL specifying the destination of the request
data *
Request content
Take a look at this answer to see how to read request body.
These days it's easier to use Spring Boot or other frameworks to handle your server side endpoints. There is nothing wrong with using Servlets but you will have to write more code yourself.

How to pass large number of parameters from Angular js to rest service

I am trying to fetch parameters from angular JS $http service to rest service using **#queryParam** . I need to fetch lot of parameters(below have shown for 3 as an example ,but I need to use around 12-15 of them which I need to pass to the java side) ,so fetching all with #QueryParam makes the code look pretty bad .I am using GET.
How can I optimize this ?
Example what I am doing -
Angular Js code -
$http({
url: someUrl,
method: "GET",
params: {filter1: $scope.filter1,
filter2:$scope.filter2,
filter3:$scope.filter3
});
Java side -
#path("/getAllData")
#GET
#Produces({..}
public response getAllData(#QueryParam("filter1") final String filter1,
#QueryParam("filter2") final String filter2,
#QueryParam("filter3") final String filter3){
}
Also ,wanted to know the optimization in case when I am building URL instead of params object, and picking the same with #PathParam
$http.get('rest/test/getAllData/?filter1='$scope.filter1 +
'&filter2='$scope.filter2 + '&filter3='$scope.filter3 +
'&filter4='$scope.filter4)
I am able to do it by passing individually in #QueryParam . I am looking for optimized code when we a large number of parameters.
Create a POJO with all the required parameters.
In angular, do this
var obj = {};
obj.filter1 = $scope.filter1;
obj.filter2 = $scope.filter2;
obj.filter3 = $scope.filter3;
$http({
url: someUrl,
method: "GET",
params: obj
});
You can accept all the parameters in you rest like this -
#path("/getAllData")
#GET
#Produces({..}
public response getAllData(MyPojo obj){
//String filter1 = obj.filter1;
}
You can do it in 2 ways:
1) org.json.simple.JSONObject.
2) Bean or POJO Class.
AngularJS Controller:
var URL = appURL+'/adm/addCollProcess.do';
var json = {"col_pro_id":$scope.col_pro_id, "col_code": $scope.col_code, "exam_type_ids": $scope.exam_types.toString().replace("[","").replace("]",""),
"created_by" : "admin", "file_path" : $scope.file_path, "website" : $scope.website, "facebook" : $scope.facebook};
// using JSONObject
$http.post(URL, json).then(function(response){
if(response.data){
// Code
}
});
// using Bean Class
$http.post(URL, JSON.stringify(json)).then(function(response){
if(response.data){
// Code
}
});
Java Controller:
// using JSONObject
#RequestMapping(value="/addCollProcess.do", method=RequestMethod.POST)
public boolean addCollProcess(#RequestBody JSONObject json){
// Code
}
// using Bean Class:
#RequestMapping(value="/addCollProcess.do", method=RequestMethod.POST)
public #ResponseBody boolean addCollProcess(#RequestBody AdmissionProcessBean processBean) {
// Code
}

vertx get body of a simple post reques

Their doc on vertx website isn't quite clear on how to receive the body of a request.
var vertx = Vertx.vertx();
var server = vertx.createHttpServer();
var Router = require("vertx-web-js/router");
var BodyHandler = require("vertx-web-js/body_handler");
var router = Router.router(vertx);
router.route().handler(BodyHandler.create().handle);
router.route('POST', "/a").handler(function (routingContext) {
var response = routingContext.response();
response.setChunked(true);
response.write("a json received");
var str = routingContext.getBodyAsJson()
console.log(str);
// Now end the response
routingContext.response().end();
});
I get the error:
vertx-js/util/console.js:9 ReferenceError: "inspect" is not defined
How am I supposed to know what to call if they don't even put it in their doc..
Paulo said my version of vertx was outdated and this was a bug. I'll take his word for it. In the meantime I tried doing it in Java like in the answer I got. However I had more success doing it like this:
router.route().handler(BodyHandler.create());
router.route(HttpMethod.POST, "/iamonline").handler(rc -> {
JsonObject json = rc.getBodyAsJson();
System.out.println(json.getString("id"));
HttpServerResponse response = rc.response();
response.putHeader("content-type", "application/json");
// Write to the response and end it
response.end("{\"status\": 200}");
});
I ran into the same the first time. Use the .bodyHandler which is a convenience method for receiving the entire request body in one piece.
As a reference, I'll give you an example in Java (you can easily "transform" it into ECMAScript):
public void login(final RoutingContext routingContext) {
routingContext.request().bodyHandler(bodyHandler -> {
final JsonObject body = bodyHandler.toJsonObject();
// `body` now contains you what you POST'ed
});
}

org.springframework.web.client.RestClientException: Could not extract response no suitable HttpMessageConverter found

I was fetching data from a CouchDB view using spring boot and everything worked fine, I then copy-and-pasted the json content provided by the view in a .json file and provided it in a URL using NodeJS and I know get the followin error message:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class MyClass] and content type [application/octet-stream]
Here is the line of code where the error occurs:
RestTemplate myRestTemplate = new RestTemplate();
ResponseEntity<MyClass> loadRecordResponse = myRestTemplate.getForEntity("http://localhost:4000/", MyClass.class);
Here is the code I use in NodeJS to create my REST web service:
var fs = require('fs');
var express = require('express');
var app = express();
app.get('/', function (req, res) {
fs.readFile('D:\\Userfiles\\x\\Desktop\\Local CouchDB\\loads.json', 'utf8', function (err, data) {
console.log( data );
res.end( data );
});
})
var server = app.listen(4000, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})

Return Json from Spring Controller After Uploading File via AngularJS

FrontEnd: jsp with AngularJS
BackEnd: Spring MVC/Java
I am uploading a file using ng-flow, angularJS. Source: https://github.com/flowjs/ng-flow
File upload is successful. I need to return a json from my Spring Controller. Any clues how to go about it?
P.S. can't find where to put in .success() function, if at all that is applicable.
Spring Controller:
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public String uploadFileHandler(#RequestParam("file") MultipartFile file, Model model) {
//Upload file and process
JsonObject jo = Json.createObjectBuilder().add(path, folderPath.toString())
.add(aContentsAttrib, aContents)
.add(bContentsAttrib, bContents).build();
}
app.js code:
(function() {
var app = angular.module('app', ['flow'])
.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
target: 'upload',
permanentErrors: [404, 500, 501],
maxChunkRetries: 4,
chunkRetryInterval: 500,
simultaneousUploads: 4
};
flowFactoryProvider.on('catchAll', function (event) {
console.log('catchAll', arguments);
});
// Can be used with different implementations of Flow.js
// flowFactoryProvider.factory = fustyFlowFactory;
}]);
app.controller('PageController', function() {
//this.products = gems;
});
app.controller("TabController", function() {
this.tab = 1;
this.showOutput = false;
this.viewEvents = false;
this.isSet = function(checkTab) {
return this.tab === checkTab;
};
this.changeVal = function() {
this.viewEvents = true;
};
this.setTab = function(setTab) {
this.tab = setTab;
};
});
})();
What exactly should be returned from the spring controller? (String/#ResponseBody String etc)
How to collect that json in angular?
On your controller #ResponseBody should be added and the jo returned as String:
#RequestMapping(value = "/upload", method = RequestMethod.POST)
public #ResponseBody String uploadFileHandler(#RequestParam("file") MultipartFile file, Model model) {
//Upload file and process
JsonObject jo = Json.createObjectBuilder().add(path, folderPath.toString())
.add(aContentsAttrib, aContents)
.add(bContentsAttrib, bContents).build();
return jo.toString();
}
In AngularJS, you should do this for being able to post files and then retrieve the data back:
$http({url: '/url',
method: 'POST',
data: $scope.myFile,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).success(data){
$scope.myData = data;
});
In your Spring controller you should just return an Object containing the properties you want to transfer to your angular service. This will be automatically (or by default) be converted to JSON. #RequestBody is not needed.
This return value will be available in the success callback, something like:
$http({
method: 'POST',
url: '...',
}).success(function (data) {
//data is your JSON response
})},
If you are using Spring 3 you can do this
#RequestMapping(value = "/getDealers", value = "/upload", method = RequestMethod.POST)
#ResponseBody
public String uploadFileHandler() {
}
#ResponseBody annotation directly writes the response to the response stream. You would not need a JSP. Just send the request for the controller from the browser & the controller method will write the response to the response stream.
You can parse the response using Jquery or any json library & display in the JSP
Check this out
An alternate way, which I just found out. Will be useful to extract from existing code, without any modification. It does introduce an extra global variable, outside your main angular app, and might not be highly recommended, but still, posting this.
var json = {};
var app = angular.module('app', ['flow'])
.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
target: 'processxls',
permanentErrors: [404, 500, 501],
maxChunkRetries: 4,
chunkRetryInterval: 500,
simultaneousUploads: 4
};
flowFactoryProvider.on('catchAll', function (event) {
console.log('catchAll', arguments);
this.jsonResponse = arguments[2]; //Note this change
//json = this.jsonResponse;
console.log(this.jsonResponse);
json = angular.fromJson(this.jsonResponse);
});
// Can be used with different implementations of Flow.js
// flowFactoryProvider.factory = fustyFlowFactory;
}]);
'json' variable now has the json response received. You can use it for further use now.
P.S. in order to check for which value of 'i' arguments[i] gives you the json, see console.

Categories