I am trying to communicate spring MVC with angular, I send data from angular to spring but not the other way around, I have a login form, which I collect the data and send it to my JAVA project.
This works well, now I intend to return the same data to the index to show it below the form:
Here my JAVA code, the first function saveLogin
receives the JSON well, when doing a test with the same data, I send them with Loadlogin_json(user);
to the function that supposedly has to return it to the index to show it.
@Controller
public class RespuestaLogin {
@RequestMapping(value = "/savelogin_json" , method = RequestMethod.POST,
consumes="application/json",headers = "content-type=application/json")
public @ResponseBody String saveLogin(HttpServletRequest request) throws IOException{
BufferedReader reader = request.getReader();
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(reader, User.class);
Loadlogin_json(user);
return "success";
}
@RequestMapping(value = "/index", method = RequestMethod.POST)
@ResponseBody
public void Loadlogin_json( User user)throws IOException {
/*System.out.println("nombre: "+user.getName());
return new ResponseEntity<User>(user, HttpStatus.OK);*/
ObjectMapper mapper = new ObjectMapper();
try {
File json = new File("index.json");
mapper.writeValue(json, user);
System.out.println("Java object converted to JSON String, written to file");
System.out.println(mapper.writeValueAsString(user));
new ResponseEntity<ObjectMapper>(mapper, HttpStatus.OK);
} catch (JsonGenerationException ex) {
ex.printStackTrace();
} catch (JsonMappingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Next I show the code of angular, like the LoginService
of above works well, but the RespuestaLoginService
no, I attach the code to see if you can give me a hand with why I do not pick up in angular the JSON sent .
var app= angular.module('login.loginService',[]);
app.factory('LoginService', ['$http','$q', function($http, $q){
var self={
login: function(datos){
var dataObj = {
name : datos.usuario,
pass : datos.pass,
};
var d=$q.defer();
function genericSuccess (res) {
return res.data.data; // yes, really.
}
var res= $http.post('savelogin_json', dataObj).then(function(success) {
return genericSuccess(success);
});
return d.promise;
}
};
return self;
}]);
app.factory('RespuestaLoginService', ['$http','$q', function($http, $q){
console.log("entra en angular");
var self={
respuestalogin: function(datos){
var d=$q.defer();
function genericSuccess (res) {
return res.data.data; // yes, really.
}
$http.post("index",datos).then(function(success) {
return genericSuccess(success);
});
console.log("FUE LLAMADO desde el servicio RespuestaLoginService login de angularjs");
return d.promise;
}
};
return self;
}]);