我有针对客户的每个请求返回给客户的课程响应,我的响应是:

public class Response<T>  extends Request{

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


在我的服务器端,我有返回响应的方法,其中包含结果与InstagramUser的arraylist

我的方法:

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;
    }


和我的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;

    ...

}


但是在客户端,当我从服务器获取响应时,我的结果是由InstagramUser的ArrayList插入的LinkedHashMap的ArrayList:

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


这是我从服务器调用此方法的json响应:

    {
  "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
}


我怎样才能解决这个问题?

最佳答案

您将必须使用参数化类型引用。仅适用于其余模板交换方法。

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


根据您的输入调整参数。

09-11 07:30