将Json数据从角度JS传递到spring REST API并处理它

时间:2022-02-24 09:48:01

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).

我正在学习AngularJS和Spring MVC。我陷入了这样的境地:我有一个angularjs控制器,我从它对spring服务执行REST API调用。我想知道如何在控制器中接受这些JSON值并映射到模型(getter和setter)。

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.

在对数据库进行插入之前,我正在对spring进行一些验证。我还想知道如何将这些错误消息返回给angula JS并在屏幕上填充它们。

This is my AngularJS controller:

这是我的AngularJS控制器:

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.

我可以打印系统。现在我想知道如何继续下去。

1 个解决方案

#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;
    }
}

#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;
    }
}