1

I have JSON response from WS:

[
  {
    "name": "Bobby",
    "status": "single"
  },
  {
    "name": "John",
    "status": "married"
  }
]

Here is my wrapper

@XmlRootElement(name = "users")
public class UserListWrapper {   

    private List<User> users;   

    @XmlElement(name = "user")
    public List<User> getUsers() {
        return users;
    }    

    // getters and setters omitted
}

And User class

@XmlRootElement
class User {
    private String name;
    private String status;    

    // getters and setters omitted
}

The problem is when Jersey try to deserialize response to my wrapper object. It say

Caused by: org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.jersey.test.UserListWrapper out of START_ARRAY token

Seams that something wrong with my wrapper annotations. How can I fix them?

UPD

When I send

{
  "user": [
    {
      "name": "Bob",
      "status": "single"
    },
    {
      "name": "Mike",
      "status": "married"
    }
  ]
}

all works fine. But I need this format

[
  {
    "name": "Bobby",
    "status": "single"
  },
  ...
]

UPD

Jersey Client code

    HttpAuthenticationFeature authenticationFeature = HttpAuthenticationFeature.basic("user", "secret");
    Client client = ClientBuilder
            .newClient()
            .register(authenticationFeature)
            .register(JacksonFeature.class);

    WebTarget target = client.target("http://localhost:8080/server/");
    UserListWrapper entity;
    Response resp;

    resp = target.queryParam("u", "info")
            .path("/rest/users")
            .request()
            .accept(APPLICATION_JSON)
            .get();


    entity = resp.readEntity(UserListWrapper.class);
barbara
  • 3,111
  • 6
  • 30
  • 58

1 Answers1

1

Forget the UserListWrapper wrapper then. List<User> is perfect for the JSON array ( [] ) format. If you add the wrapper class, then yes you will need the extra JSON object layer ( {} ). This:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createBook(List<User> users) {

is supported just fine (at least with Jackson - which you are using).


UPDATE

If the response from the server is coming as a JSON array, then you can still deserialize it as a List<User>. For example

WebResource resource = client.resource("...");
List<User> users = resource.get(new GenericType<List<User>>(){});

See this related post


UPDATE 2

Since you are using the JAX-RS 2 client API, you can use the overloaded readEntity, which accepts a GenericType argument also

List<User> user = response.readEntity(new GenericType<List<User>>(){});
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • But I cannot modify Web Service. It sends me `[ { "name": "Bobby", "status": "single" }, ... ]` – barbara Dec 25 '14 at 02:34
  • And the resource method accepts `UserListWrapper` type? – Paul Samsotha Dec 25 '14 at 02:35
  • Yes that's right. But I can make changes to wrapper. It is on the client. – barbara Dec 25 '14 at 02:36
  • What I don't get is if the service expects `{ "users" : [ {},{} ] }`, then why don't you just send it like that? – Paul Samsotha Dec 25 '14 at 02:38
  • You seem to be presenting two different problems: one with sending and one with receiving. That is what's got me confused. Can you please clarify – Paul Samsotha Dec 25 '14 at 02:40
  • No, server expects `[ { "name": "Bobby", "status": "single" }, ... ]` But I know only how to send `{ "user": [ { "name": "Bob", "status": "single" }, { "name": "Mike", "status": "married" } ] }` – barbara Dec 25 '14 at 02:40
  • Ah I see. Why don't you just send a `List`? instead of `UserListWrapper`? If you are having problems sending the `List`, can you should the code where you are making this request – Paul Samsotha Dec 25 '14 at 02:46
  • I use wrapper also to get users from server. I need some wrapper to store the result. – barbara Dec 25 '14 at 02:49
  • You can use `List` to deserialize also. Are you using the Jersey Client API to automatically deserialize, or are you using Jackson to deseraize? – Paul Samsotha Dec 25 '14 at 02:50
  • I use Jersey Client API. – barbara Dec 25 '14 at 02:54