AngularJS Client Side:
userTab.factory('someService', ['apiService','$rootScope', function(apiService, $rootScope){
return {
updateData : function (someObj){
return apiService.request({
apiMethod: 'user/v1/updateData',
httpMethod: 'POST',
fileData: JSON.stringify(someObj)
}).error(function(data, status) {
throw "updateData error: " + status;
});
}
}
}]);
Server Side Code:
Update.java
@Controller("user")
@RequestMapping(value = "/user/v1")
public interface Update
{
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
@RequestMapping(value = "/users/{username}/updateData", method = POST)
String updateData(HttpServletRequest request, HttpServletResponse response, @PathVariable("username") String username, @RequestBody UserUpdate userUpdate);
}
UpdateImpl.java
@Override
@Transactional
public String updateData(HttpServletRequest request, HttpServletResponse response, @PathVariable("username") String username,
@RequestBody UserUpdate userUpdate) {
String requestBody = ServletUtils.getRequestBody(request);
logger.info("RequestBody: "+requestBody);
return new String("true");
}
Response: RequestBody: {}
So as you see that RequestBody
is coming as blank as I haven't given anything in params within angularjs API call.
So how to get the fileData
which is not in params here?
UPDATE:
My fileData
is a normal json array just like this:
{
"todo" : "Dinner",
"user" : [
{
"name" : "username",
"password" : "passwordabc"
}
],
"notes : "some notes"
}
And I have created it in an angularjs as follows:
var object = {};
object.todo = "Dinner";
object.notes = "some notes";
userArray = [{
name: 'myname',
password: 'password'
}];
object.user = userArray
UPDATE:
apiService.js
'use strict';
apiModule.factory('apiService', ['$http', '$q', 'LoginService','XSSValidator', function($http, $q, loginService, XSSValidator) {
var basePath="https://somesite.com/userApi/";
var _httpMethod="POST";
var caching=false;
var latestRequest={};
$http.defaults.withCredentials = true;
return{
if(typeof bundle.fileData != "undefined"){
return $http({
cache: caching,
method: bundle.httpMethod,
headers:{"Content-Type":contentType},
url:basePath+bundle.apiMethod,
data:dataStr,
transformRequest: angular.identity,
transformResponse:bundle.transformResponse,
timeout: canceller.promise
}).success(function(data, status, headers, config) {
//check to see if request has been redirected to login page, suggesting user has been logged out
if(typeof data==='string' && data.substr(0,44)==='<html><head><title>Login Page</title></head>'){
window.location.href="/login" + window.location.search;
}
var req;
if(config.data !== ""){
var requestArr=config.url.split(config.data);
req=requestArr[0].split("?")[0];
}
else{
req=config.url;
}
if(config.data !== "" && !data.fault){
if(Object.prototype.toString.call(data) == "[object String]"){
var msg = XSSValidator.validate("Response ", data);
if (msg != null) {
return $q.reject({ message: msg });
}
}else if(config.headers["Content-Type"] == "application/x-www-form-urlencoded" || config.headers["Content-Type"] == "application/json"){
eval(data);
}
}
if(latestRequest[req]!==config.url){
//cancel promise
return $q.reject({ message: 'Rejecting this promise' });
}
}).error(function(data, status, headers, config) {
if(status===401){ //user has been logged out server-side. redirect to login pg
window.location.href="/login" + window.location.search;
}
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}