angularjs - 将 Json 数据从 Angular JS 传递到 Spring REST API 并处理它

标签 angularjs json spring spring-mvc

我正在学习 AngularJSSpring MVC。我陷入了这样一种情况:我有一个 angularjs Controller ,我可以从中对 spring 服务进行 REST API 调用。我想知道如何在 Controller 内接受这些 JSON 值并映射到模型(getter 和 setter)。

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

这是我的 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' 
            }
        });
    }
}); 

这是我的 Spring 服务:

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

我能够打印系统输出。现在我想知道如何继续。

最佳答案

enter link description here

像这样。

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

关于angularjs - 将 Json 数据从 Angular JS 传递到 Spring REST API 并处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40836439/

相关文章:

javascript - 将 AngularJS 表单发送到 NodeMailer 以发送电子邮件

angularjs - Grunt 非常慢 - 100% cpu

javascript - ng-model 防止选中 ="checked"默认选择单选按钮

javascript - 将 JSON 数组从 Django View 返回到模板

javascript - $http - header 未返回 - AngularJS

java - OpenCSV + RestTemplate : Array-Types

javascript - 以 Angular 显示数据

javascript - 如何在 Java 中将 PostGIS 数据库中的数据转换为 GeoJSON

java - Spring MVC 应用程序在 Eclipse 中运行但不在 Intellij 中运行 - 找不到 404

java - 使用 Hibernate JPA 启动 Spring Boot 应用程序时出现 NoSuchMethodError