Java 如何将JSON数组从AngularJS控制器传递到Spring控制器?

Java 如何将JSON数组从AngularJS控制器传递到Spring控制器?,java,angularjs,json,spring-mvc,Java,Angularjs,Json,Spring Mvc,我正在用AngularJS和Hibernate 5在春天做水疗 将JSON数组从AngularJS控制器传递到Spring控制器时出错 所有字段值都成功地进入angular JSON数组,但没有传入Spring控制器 错误:无法读取JSON:;嵌套异常是 com.google.gson.JsonSyntaxException: 我的项目结构如下 Spring\u Hibernate\u MVC =src -com->karmesh->mvcApp->controller->register->R

我正在用AngularJS和Hibernate 5在春天做水疗

将JSON数组从AngularJS控制器传递到Spring控制器时出错

所有字段值都成功地进入angular JSON数组,但没有传入Spring控制器

错误:无法读取JSON:;嵌套异常是 com.google.gson.JsonSyntaxException:

我的项目结构如下

Spring\u Hibernate\u MVC =src -com->karmesh->mvcApp->controller->register->RegisterController.java =网络内容 -js->app->RegisterController.js -视图->Register.html

注册,html

<div id="DivRegisterMain" ng-controller="RegisterController">   
    <form name="myForm" novalidate>

:::://Form fields here.
        <input type="submit" value="SubmitTest" ng-click="submit()" ><br>
    </form>

</div>
RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {

    $scope.regJson = {
        "is" : 1,   
        "fname" : "",
        "lname" : "",
        "gender" : "",
        "dob" : "",
        "email" : "",
        "contact" : "",
        "yop" : "",
        "degree" : "",
        "branch" : "",
        "perc" : "",
        "state" : "",
        "city" : ""

    };

$scope.studentList = [];

$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: $scope.studentList,

        };

        $http(req).
        then(function(response){
            console.log(response); // prints true or false
            if (response)
              console.log("in success");
            else 
               console.log("in fail");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in error");

        });


    };
$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: angular.toJson($scope.studentList),// note this

        };

    };
@ResponseBody
    @RequestMapping(value = "/registerStudent.do", method = RequestMethod.POST)
    public boolean registerStudent(@RequestBody String data) {

        Gson googleJson = new Gson();
        ArrayList stdList = googleJson.fromJson(data, ArrayList.class);

        if (stdList != null) {
            // store your stdList
        }
        return registerService.isStudentExist(stdList);

    }
RegisterController.java

@EnableWebMvc
@RestController
@RequestMapping("/")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    public RegisterController() {
        System.out.println(this.getClass().getSimpleName() + "created..");
    }


    @ResponseBody
    @RequestMapping(value="/registerStudent.do", method = RequestMethod.POST)   
    public boolean registerStudent(@RequestBody List<RegisterDTO> stdList) {    
        System.out.println("inside controller..");

        if (stdList != null) {  
        System.out.println("success...");
          }
          return registerService.isStudentExist(stdList);

    }
}
@EnableWebMvc
@RestController
@请求映射(“/”)
公共类注册控制器{
@自动连线
专用注册服务注册服务;
公共注册表控制器(){
System.out.println(this.getClass().getSimpleName()+“created..”;
}
@应答器
@RequestMapping(value=“/registerStudent.do”,method=RequestMethod.POST)
公共布尔寄存器学生(@RequestBody List stdList){
System.out.println(“内部控制器”);
如果(stdList!=null){
System.out.println(“成功…”);
}
返回registerService.isStudentExist(stdList);
}
}

使用JSON序列化/反序列化

你的请求应该是

            var req = {
                 method: 'POST',    url:'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: JSON.stringify($scope.studentList),
           };
您的spring控制器

    @ResponseBody
    @RequestMapping(value="/registerStudent.do", method = RequestMethod.POST)   
    public boolean registerStudent(@RequestBody string data) { 
        List<RegisterDTO> stdList = JsonConvert.DeserializeObject<RegisterDTO>(data); // find java jsondeserializer
        System.out.println("inside controller..");

        if (stdList != null) {  
            System.out.println("success...");
         }
          return registerService.isStudentExist(stdList);

    }  
@ResponseBody
@RequestMapping(value=“/registerStudent.do”,method=RequestMethod.POST)
公共布尔寄存器学生(@RequestBody字符串数据){
List stdList=JsonConvert.DeserializeObject(数据);//查找java jsondeserializer
System.out.println(“内部控制器”);
如果(stdList!=null){
System.out.println(“成功…”);
}
返回registerService.isStudentExist(stdList);
}  

使用JSON序列化/反序列化

你的请求应该是

            var req = {
                 method: 'POST',    url:'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: JSON.stringify($scope.studentList),
           };
您的spring控制器

    @ResponseBody
    @RequestMapping(value="/registerStudent.do", method = RequestMethod.POST)   
    public boolean registerStudent(@RequestBody string data) { 
        List<RegisterDTO> stdList = JsonConvert.DeserializeObject<RegisterDTO>(data); // find java jsondeserializer
        System.out.println("inside controller..");

        if (stdList != null) {  
            System.out.println("success...");
         }
          return registerService.isStudentExist(stdList);

    }  
@ResponseBody
@RequestMapping(value=“/registerStudent.do”,method=RequestMethod.POST)
公共布尔寄存器学生(@RequestBody字符串数据){
List stdList=JsonConvert.DeserializeObject(数据);//查找java jsondeserializer
System.out.println(“内部控制器”);
如果(stdList!=null){
System.out.println(“成功…”);
}
返回registerService.isStudentExist(stdList);
}  

您的请求中缺少
contentType:'application/json'

您的请求中缺少
contentType:'application/json'

RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {

    $scope.regJson = {
        "is" : 1,   
        "fname" : "",
        "lname" : "",
        "gender" : "",
        "dob" : "",
        "email" : "",
        "contact" : "",
        "yop" : "",
        "degree" : "",
        "branch" : "",
        "perc" : "",
        "state" : "",
        "city" : ""

    };

$scope.studentList = [];

$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: $scope.studentList,

        };

        $http(req).
        then(function(response){
            console.log(response); // prints true or false
            if (response)
              console.log("in success");
            else 
               console.log("in fail");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in error");

        });


    };
$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: angular.toJson($scope.studentList),// note this

        };

    };
@ResponseBody
    @RequestMapping(value = "/registerStudent.do", method = RequestMethod.POST)
    public boolean registerStudent(@RequestBody String data) {

        Gson googleJson = new Gson();
        ArrayList stdList = googleJson.fromJson(data, ArrayList.class);

        if (stdList != null) {
            // store your stdList
        }
        return registerService.isStudentExist(stdList);

    }
下载gsonjar文件

RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {

    $scope.regJson = {
        "is" : 1,   
        "fname" : "",
        "lname" : "",
        "gender" : "",
        "dob" : "",
        "email" : "",
        "contact" : "",
        "yop" : "",
        "degree" : "",
        "branch" : "",
        "perc" : "",
        "state" : "",
        "city" : ""

    };

$scope.studentList = [];

$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: $scope.studentList,

        };

        $http(req).
        then(function(response){
            console.log(response); // prints true or false
            if (response)
              console.log("in success");
            else 
               console.log("in fail");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in error");

        });


    };
$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: angular.toJson($scope.studentList),// note this

        };

    };
@ResponseBody
    @RequestMapping(value = "/registerStudent.do", method = RequestMethod.POST)
    public boolean registerStudent(@RequestBody String data) {

        Gson googleJson = new Gson();
        ArrayList stdList = googleJson.fromJson(data, ArrayList.class);

        if (stdList != null) {
            // store your stdList
        }
        return registerService.isStudentExist(stdList);

    }
RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {

    $scope.regJson = {
        "is" : 1,   
        "fname" : "",
        "lname" : "",
        "gender" : "",
        "dob" : "",
        "email" : "",
        "contact" : "",
        "yop" : "",
        "degree" : "",
        "branch" : "",
        "perc" : "",
        "state" : "",
        "city" : ""

    };

$scope.studentList = [];

$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: $scope.studentList,

        };

        $http(req).
        then(function(response){
            console.log(response); // prints true or false
            if (response)
              console.log("in success");
            else 
               console.log("in fail");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in error");

        });


    };
$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: angular.toJson($scope.studentList),// note this

        };

    };
@ResponseBody
    @RequestMapping(value = "/registerStudent.do", method = RequestMethod.POST)
    public boolean registerStudent(@RequestBody String data) {

        Gson googleJson = new Gson();
        ArrayList stdList = googleJson.fromJson(data, ArrayList.class);

        if (stdList != null) {
            // store your stdList
        }
        return registerService.isStudentExist(stdList);

    }
下载gsonjar文件

RegisterController.js

routeApp.controller("RegisterController", function($scope, $http) {

    $scope.regJson = {
        "is" : 1,   
        "fname" : "",
        "lname" : "",
        "gender" : "",
        "dob" : "",
        "email" : "",
        "contact" : "",
        "yop" : "",
        "degree" : "",
        "branch" : "",
        "perc" : "",
        "state" : "",
        "city" : ""

    };

$scope.studentList = [];

$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: $scope.studentList,

        };

        $http(req).
        then(function(response){
            console.log(response); // prints true or false
            if (response)
              console.log("in success");
            else 
               console.log("in fail");
            $scope.studentList=[];
        }, function(response){
            console.log(response.status);
            console.log("in error");

        });


    };
$scope.submit = function() {

        var req = {
                 method: 'POST',                 
                 url: 'http://localhost:8050/Spring_Hibernate_MVC/registerStudent.do',              
                 data: angular.toJson($scope.studentList),// note this

        };

    };
@ResponseBody
    @RequestMapping(value = "/registerStudent.do", method = RequestMethod.POST)
    public boolean registerStudent(@RequestBody String data) {

        Gson googleJson = new Gson();
        ArrayList stdList = googleJson.fromJson(data, ArrayList.class);

        if (stdList != null) {
            // store your stdList
        }
        return registerService.isStudentExist(stdList);

    }

这个exist JsonConvert.DeserializeObject(which.jar)在哪里?在c#中,搜索它并查找java JSONderializer。这个exist JsonConvert.DeserializeObject(which.jar)在哪里?在c#中,搜索它并查找java JSONderializer。我试过这个。我不需要我的编码。它已经在传递contentType:'application/json'。我尝试了这个。我不需要我的编码。它已经在传递contentType:“application/json”。