簡體   English   中英

如何將JSON數組從AngularJS控制器傳遞給Spring Controller?

[英]How to pass JSON array from AngularJS Controller to the Spring Controller?

我正在使用AngularJS在Spring 4中使用Hibernate 5制作SPA。

我將JSON數組從AngularJS控制器傳遞給Spring Controller時出錯。

所有字段值都成功地出現在角度JSON數組中,但沒有傳入Spring控制器。

錯誤:無法讀取JSON:; 嵌套異常是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>

app.js

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

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

        });


    };

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

    }
}

使用JSON序列化/反序列化

你的要求應該是

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

你的彈簧控制器

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

    }  

您在請求中缺少contentType: 'application/json'

RegisterController.js

$scope.submit = function() {

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

        };

    };

下載gson jar文件。

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM