首页 > 解决方案 > 如何使用 jersey REST Webtarget API 发布对象的 json 列表?- 得到错误 MessageBodyWriter not found

问题描述

我可以使用以下代码发送单个对象:

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

但是,这不起作用:

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


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

我收到以下错误:

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

标签: javajsonrestjerseyjax-rs

解决方案


不确定这是否可行,但您可以尝试使用数组而不是列表。

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

推荐阅读