简体   繁体   中英

How do you POST a json list of objects using the jersey REST Webtarget API? - getting error MessageBodyWriter not found

I can send a single object fine with the following code:

        Entity<User> body = Entity.json(user);
        Response response = webTarget.path("/singleuser")
          .request(MediaType.APPLICATION_JSON)
          .post(body);

however, this does not work:

        Entity<List<User>> body = Entity.json(users);


        Response response = webTarget
                .path("/multipleusers")
                .request(MediaType.APPLICATION_JSON)
                .post(body);

I get the following error:

MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList

Not sure this will work, but you can try array instead of list.

Entity<User[]> body = Entity.json(users);
        Response response = webTarget
                .path("/multipleusers")
                .request(MediaType.APPLICATION_JSON)
                .post(body);

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