send JSON from Spring MVC to angularjs

1

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;


}]);
    
asked by nose ke 10.01.2017 в 11:52
source

2 answers

1

Good morning,

Angular does not receive anything because those services are not responding at all. You must indicate what services are going to return. One of them returns void and the other String with a "success".

    
answered by 16.01.2017 в 17:23
1

Angular requests are made through AJAX, so you need to write down your controller as @RestController and directly return the object you want.

For example, if you want to return the same object you send:

@RestController
public class RespuestaLogin {

  @RequestMapping(value = "/index", method = RequestMethod.POST)
  public  User Loadlogin_json(@RequestBody User user)throws IOException {

    return user;

  }

}

In the Angular controller you could see the response :

$http.post("index",datos).then(function(response) {
  return console.log(response);
});
    
answered by 08.05.2017 в 17:48