我正在使用AngularJS在Spring 4中使用Hibernate 5进行SPA。
我在将JSON数组从AngularJS控制器传递给Spring Controller时遇到错误。
所有字段值成功进入角度JSON数组,但未传入Spring控制器。
错误:无法读取JSON:;嵌套异常是 com.google.gson.JsonSyntaxException:
我的项目结构如下所示。
Spring_Hibernate_MVC SRC = -com-> karmesh-> mvcApp->控制器 - >登记册> 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);
}
}
答案 0 :(得分:0)
使用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);
}
答案 1 :(得分:0)
您的请求中缺少contentType: 'application/json'
!
答案 2 :(得分:0)
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);
}