[英]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 在我的服务器端,我有方法返回包含InstagramUser的 arraylist结果的Response
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 : 和我的Instagram用户 :
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 : 但在客户端,当我从服务器获取我的结果是响应的LinkedHashMap的 ArrayList的insted的InstagramUser的ArrayList中的:
restTemplate.postForObject(URL + conditions, params,Response.class);
and this is my json response from server for calling this method: 这是我从服务器调用此方法的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
}
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 ). 您的Response类,因此切换到默认类型(可能是LinkedHashMap )。
Maybe the answer to the following SO-question is of any help: 也许以下SO问题的答案有任何帮助:
Using Spring RestTemplate in generic method with generic parameter 在泛型参数的泛型方法中使用Spring RestTemplate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.