简体   繁体   中英

Java REST and ANGULAR > JSON parse error: Cannot deserialize instance of `java.util.ArrayList`

I am facing this error when I try to send a Object using JSON to my Java controller. JSON parse error: Cannot deserialize instance of java.util.ArrayList

My model.

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

  private static final long serialVersionUID = 1L;

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

My Controller

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

My Js service

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

My JSON

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

My 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 />

Thanks

This json from UI seems like a map not an ArrayList :

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

if you really want to parse it as ArrayList make sure that the request from UI should be :

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

If my understanding is wrong, please share the pretty JSON coming from the UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM