简体   繁体   中英

MappingJackson2HttpMessageConverter Mistake on Converting Complex Object

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?

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.

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

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