有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

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

Error: Could not read JSON: ; nested exception is com.google.gson.JsonSyntaxException:

我的项目结构如下

Spring_Hibernate_MVC =src -com->karmesh->mvcApp->controller->register->RegisterController.java =WebContent -js->app->RegisterController.js -Views->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>

应用程序。js

var routeApp=angular.module("RouteApp",['ngRoute']);

注册控制器。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");

        });


    };

注册控制器。爪哇

@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);

    }
}

共 (3) 个答案

  1. # 1 楼答案

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

  2. # 2 楼答案

    注册控制器。js

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

    下载gsonjar文件

    注册控制器。js

    @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);
    
        }
    
  3. # 3 楼答案

    使用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);
    
        }