1

i have Class Response that return to client for every request from client my Response is:

public class Response<T>  extends Request{

    private ResponseType responseType;
    private String message;
    private ArrayList<T> result;
    private int activationCode;
    .
    .
    .
}

in my server side i have method that return Response that contains results with arraylist of InstagramUser

my method:

public Response getUserByUserName(@RequestBody List<Request> requests){
        .
    .
    .
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        try {
            String searchResponse = restTemplate.getForObject(uri, String.class);
            JSONObject rawObject = new JSONObject(searchResponse);
            JSONArray searchResults = rawObject.getJSONArray("data");
            ArrayList<InstagramUser> users = new ArrayList<InstagramUser>();
            for (int i = 0; i < searchResults.length(); i++) {
                JSONObject result = searchResults.getJSONObject(i);
                users.add(new InstagramUser(result, AccessToken.getTokenString()));
            }
            response = new Response(requests.get(0).getUserId(), ResponseType.success, "users find successfully on: " + new Date().toString());
            response.setResult(users);
        .
            .
            .
        return response;
    }

and my InstagramUser:

public class InstagramUser extends InstagramModel {

    protected long id;
    protected String userName;
    protected String fullName;
    protected String profilePictureURI;
    protected String bio;
    protected String website;
    protected int mediaCount = -1;
    protected int followerCount = -1;
    protected int followingCount = -1;

    ...

}

but in client side when i get Response from server my results is ArrayList of LinkedHashMap insted of ArrayList of InstagramUser:

restTemplate.postForObject(URL + conditions, params,Response.class);

and this is my json response from server for calling this method:

    {
  "id": 6151638910251304448,
  "userId": 2,
  "instagramId": null,
  "searchId": null,
  "mediaId": null,
  "phoneNumber": null,
  "name": null,
  "date": 1466665008687,
  "responseType": "success",
  "message": "users find successfully on: Thu Jun 23 11:26:48 IRDT 2016",
  "result": [
    {
      "id": 110000004535,
      "userName": "______etabdar",
      "fullName": "________dar",
      "profilePictureURI": "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xap1/t51.2885-19/s150x150/13183XXXXXXXX0231_1584363729_a.jpg",
      "bio": " XXXXX 90",
      "website": "",
      "mediaCount": -1,
      "followerCount": -1,
      "followingCount": -1
    }
  ],
  "activationCode": 0
}

how can i fix this?

2 Answers2

3

you'll have to use parameterized type reference. Works only with rest template exchange methods.

List<YourObjectType>> res = template.exchange(
    rootUrl,
    HttpMethod.POST,
    null,
    new ParameterizedTypeReference<List<YourObjectType>>() {});

Adjust parameters based on your inputs.

s7vr
  • 73,656
  • 11
  • 106
  • 127
  • can u explain more please?? i want to get Response how can i do that? – Mehdi Akbarian Rastaghi Jun 29 '16 at 09:28
  • response=restTemplate.exchange(URL+conditions, HttpMethod.POST,null,new ParameterizedTypeReference>>() {},params); – Mehdi Akbarian Rastaghi Jun 29 '16 at 10:10
  • Error:(81, 34) error: no suitable method found for exchange(String,HttpMethod,,>>>,Object[]) method RestTemplate.exchange(String,HttpMethod,HttpEntity>,Class,Object...) is not applicable (cannot infer type-variable(s) T#2 (argument mismatch; >>> cannot be converted to Class)) method RestTemplate.exchange(String,HttpMethod,HttpEntity>,Class,Map) is not applicable (cannot infer type-variable(s) T#3 ... – Mehdi Akbarian Rastaghi Jun 29 '16 at 10:11
  • Can you try this ?ResponseEntity> responseEntity = restTemplate.exchange( URL+conditions, HttpMethod.POST, null, new ParameterizedTypeReference>() {}, params); Response response = responseEntity.getBody(); – s7vr Jun 29 '16 at 11:43
  • Is your code compiling fine? Can you paste the error that you see after this change? – s7vr Jun 29 '16 at 12:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115970/discussion-between-sagar-reddy-and-mehdi-akbarian). – s7vr Jun 29 '16 at 13:16
0

It looks like

restTemplate.postForObject(URL + conditions, params,Response.class)

doesn't know the specific type this part of your servers response:

 "result": [
{
  "id": 110000004535,
  "userName": "______etabdar",
  "fullName": "________dar",
  "profilePictureURI": "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xap1/t51.2885-19/s150x150/13183XXXXXXXX0231_1584363729_a.jpg",
  "bio": " XXXXX 90",
  "website": "",
  "mediaCount": -1,
  "followerCount": -1,
  "followingCount": -1
}

],

should be mapped to when assigning

private ArrayList<T> result;

of your Response class, therefore switching to a default type (probably LinkedHashMap).

Maybe the answer to the following SO-question is of any help:

Using Spring RestTemplate in generic method with generic parameter

Community
  • 1
  • 1
Florian Stendel
  • 359
  • 1
  • 9