Java REST和ANGULAR> JSON解析错误:无法反序列化java.util.ArrayList的实例

时间:2019-03-04 19:27:36

标签: java json angular jsp

当我尝试使用JSON将对象发送到Java控制器时,我遇到此错误。 JSON解析错误:无法反序列化java.util.ArrayList的实例

我的模特。

@Document(collection = "post")
public class Post implements Serializable {

  private static final long serialVersionUID = 1L;

  @Field("pics")
  private List<String> pics;

我的控制器

@PostMapping(value = "/savePost", produces = 
MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<Void> savePost(@RequestBody Post post) {

我的Js服务

   function savePost(post) {
    debugger;
    var deferred = $q.defer();
    $http.post(REST_SERVICE_SAVE_POST_URI,post).then(
        function (response) {
            deferred.resolve(response);
        },
        function(errResponse){
            console.log('Error while savePost posts');
            console.log(errResponse);
            deferred.reject(errResponse);
        }
    );
    return deferred.promise;
}

我的JSON

pics:
  0: "123"
  1: "123"
  2: "123"
  3: "123"
__proto__: Object

我的JSP

        <label for="postPic1" class=form-post-label><spring:message code="page.manager.post.pic1" /></label>  
        <input type="text" id="postPic1" class="form-control form-post-input" ng-model="postAdd.pics[0]" required />

        <label for="postPic2" class=form-post-label><spring:message code="page.manager.post.pic2" /></label>  
        <input type="text" id="postPic2" class="form-control form-post-input" ng-model="postAdd.pics[1]" required />

        <label for="postPic3" class=form-post-label><spring:message code="page.manager.post.pic3" /></label>  
        <input type="text" id="postPic3" class="form-control form-post-input" ng-model="postAdd.pics[2]" required />

        <label for="postPic4" class=form-post-label><spring:message code="page.manager.post.pic4" /></label>  
        <input type="text" id="postPic4" class="form-control form-post-input" ng-model="postAdd.pics[3]" required />

谢谢

1 个答案:

答案 0 :(得分:0)

UI中的json似乎是地图,而不是 ArrayList

pics: { 
 0: "123"
 1: "123"
 2: "123"
 3: "123"
}

如果您真的想将其解析为ArrayList,请确保来自UI的请求应为:

"pics": ["123", "123", "123"]

如果我的理解是错误的,请分享来自UI的漂亮JSON。