调用第三方服务接口使用RestTemplate方式,返回结果被转换为LinkedHashMap结构

调用第三方服务接口使用RestTemplate方式,返回结果被转换为LinkedHashMap结构

服务方返回的对象结构:

public class SearchGroupBaseInfoResponse{
	private List<SearchGroupBaseInfo> searchGroupBaseInfoList;
	private int totalNum;
	private int totalPage;
}

客户端调用方式:

ResponseEntity<ResponseObject> responseJson=null;
try{
        entity = restTemplate.exchange(getGroupInfosUrl, HttpMethod.POST, httpEntity, ResponseObject.class);
}catch (Exception e){
      log.error("当前时间【{}】调用UIMC获取团体用户信息服务异常,请求url={},异常信息:{}", DateUtil.getDateByDefaultPattern(new Date()), getGroupInfosUrl, e.getMessage(), e);
     throw new BusinessException(CacheHelper.getRedisErrorMessageByKey(ActivityErrorCodeConstants.ProcessErrorCode.CALL_UIMC_GROUPINFO_ERR));
        }
if(responseJson.getStatusCodeValue() != HTTP_RESPONSE_CODE||null==responseJson.getBody()){
     log.info("调用UIMC获取团体用户信息,返回数据异常,状态码:【{}】,body:【{}】",responseJson.getStatusCodeValue(),responseJson.getBody());
  throw new BusinessException(CacheHelper.getRedisErrorMessageByKey(ActivityErrorCodeConstants.ProcessErrorCode.CALL_UIMC_GET_GROUPINFO_ERR));
}
Object data=entity.getbody().getData();

单步调试的结果如下图:
在这里插入图片描述

原因:
因为RPC远程调用在底层还是使用HttpClient,所以在传递参数时,需要有个顺序,当传递map的时候map里面的值也要有顺序,不然服务层在接收的时候也会出问题,然而与Map对应有顺序的数据结构就是LinkedHashMap;spring有一个类叫ModelMap,继承了LinkedHashMap,所以一个接口返回的结果就可以直接用ModelMap来接收,ModelMap是没有范型的,不管返回的结果是什么类型的map,多么复杂的map,都可以用ModelMap来接收返回结果。
对于上述问题的解决方案:
使用ParameterizedTypeReference设置HttpRequestExecutingMessageHandler的setExpectedResponseType
代码如下:

ResponseEntity<ResponseObject<SearchGroupBaseInfoResponse>> entity=null;
 //使用这种方式转换restTemplate接收返回LinkedHashMap类型的对象
 ParameterizedTypeReference<ResponseObject<SearchGroupBaseInfoResponse>> typeRef= new ParameterizedTypeReference<ResponseObject<SearchGroupBaseInfoResponse>>() {};
 try{
            entity = restTemplate.exchange(getGroupInfosUrl, HttpMethod.POST, httpEntity, typeRef);
// responseJson= restTemplate.exchange(getGroupInfosUrl, HttpMethod.POST, httpEntity, JSONObject.class);
 }catch (Exception e){
            log.error("当前时间【{}】调用UIMC获取团体用户信息服务异常,请求url={},异常信息:{}", DateUtil.getDateByDefaultPattern(new Date()), getGroupInfosUrl, e.getMessage(), e);
 throw new BusinessException(CacheHelper.getRedisErrorMessageByKey(ActivityErrorCodeConstants.ProcessErrorCode.CALL_UIMC_GROUPINFO_ERR));
 }
        if(responseJson.getStatusCodeValue() != HTTP_RESPONSE_CODE||null==responseJson.getBody()){
            log.info("调用UIMC获取团体用户信息,返回数据异常,状态码:【{}】,body:【{}】",
 responseJson.getStatusCodeValue(),responseJson.getBody());
 throw new BusinessException(CacheHelper.getRedisErrorMessageByKey(ActivityErrorCodeConstants.ProcessErrorCode.CALL_UIMC_GET_GROUPINFO_ERR));
 }
        ResponseObject<SearchGroupBaseInfoResponse> body = entity.getBody();
 SearchGroupBaseInfoResponse data1 = body.getData();

单步调试结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值