2

I'm learning AngularJS and Spring MVC. I'm stuck in a situation where I have an angularjs controller from which I do a REST API call to a spring service. I would like to know how to accept those JSON values inside the controller and map to the Model (getters and setters).

I'm doing some validations on the spring side before doing an insert in the database. I would also like to know how to return those error messages to angula JS and populate them on the screen.

This is my AngularJS controller:

var myapp = angular.module("app");
myapp.controller("RedisplayController",function($scope,$localStorage,$http, $location,$window,) {
    $scope.addconfig = function() {    
        var dataobj = {
            escfirstname : $scope.escfirstname ,esclastname:$scope.esclastname,escage:$scope.escage 
        }
        $http({
            url: "http://localhost:8080/services/AddConfig", 
            method: "POST",
            data: dataobj,
            headers: { 
                'Accept': 'application/json',
                'Content-Type': 'application/json' 
            }
        });
    }
}); 

This is my Spring service:

@Controller
public class TestController {
    @RequestMapping(value = "/AddConfig",   method = RequestMethod.POST)
    public @ResponseBody String PostService(@RequestBody Config person) {
        System.out.println("came insidee");
        return null;
    }
}

I'm able to print the sysout. Now I would like to know how to proceed with this.

2
  • requesting url might not found try edit with localhost:8080/AddConfig Commented Nov 28, 2016 at 4:51
  • Hey i am able to connect with my services. I need to know the proper way of returning error messages from spring to Angular and display them Commented Nov 28, 2016 at 4:54

1 Answer 1

2

enter link description here

like this.

var myapp = angular.module("app");

myapp.controller("RedisplayController",function($scope,$localStorage,$http, $location,$window,) {
        $scope.addconfig = function() {    
   var dataObject = {
      escfirstname : $scope.escfirstname, 
      esclastname : $scope.esclastname, 
      escage : $scope.escage
   };

   $http({
      method: 'POST', 
      url: 'http://localhost/jsonURL',
      data: dataObject,
      headers: {'Content-Type': 'application/json; charset=utf-8'} 

   }).success(function(data, status, headers, config) {
      if( data ) {

      }
      else {

      }
   }).error(function(data, status, headers, config) {
      console.log(status);
   });
}
}); 

@Controller
public class TestController {

    @RequestMapping(value = "/AddConfig", method = RequestMethod.POST)
    public @ResponseBody String PostService(@RequestBody Config person) {

        // System.out.println("person" + person.);
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot , this is what exactly i was looking for :)
Yeah. ok, i want to check your post.
If i were u i will give score to me :)
i dont have 15 reputation to do that stilll !! :)
I am receiving bad request error when calling the service
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.