5

I am trying to send a List by GET method.

Here is my Server side:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers(){
    return managment.getUsers();
}

And my client side:

public static void getUsers(){
    try {
        ClientConfig clientConfig = new DefaultClientConfig();
        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        Client client = Client.create(clientConfig);




        WebResource webResource = client
                .resource("http://localhost:8080/Serwer07/user");

        ClientResponse response = webResource.accept("application/json")
                .get(ClientResponse.class);

        if (response.getStatus() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
        }

       List users = response.getEntity(List.class);          
       User user = (User) users.get(0);  //cannot cast
        e.printStackTrace();
    }
}

I have problem with cast from Java Object to User. How can I send this List ?

Thanks in advance.

1 Answer 1

5

Use GenericType.

List<User> users = webResource.accept(MediaType.APPLICATION_JSON)
   .get(new GenericType<List<User>>() {});

UPDATE

ClientResponse also overloads getEntity to accept GenericTypes.

List<User> users = response.getEntity(new GenericType<List<User>>() {});      
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for Your help. I have not tested the first way but the second one works great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.