服务提供方:
@RequestMapping(path = "...")
public MessageBox<Map<String, Object>> testReceive(...) {...}
请求类型、接口地址以及接口内部逻辑都不重要,重要的是接口返回的是泛型数据 MessageBox<Map<String, Object>>
服务调用方:
@PostMapping("...")
public void dealWithGenericData(...) {
...
String url = ...;
HttpEntity<String> fromEntity = ...;
ParameterizedTypeReference<MessageBox<Map<String, Object>>> typeReference = new ParameterizedTypeReference<MessageBox<Map<String, Object>>>() {};
ResponseEntity<MessageBox<Map<String, Object>>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, fromEntity, typeReference);
MessageBox<Map<String, Object>> body = responseEntity.getBody();
log.info(String.valueOf(body));
}
相关源码
<T> ResponseEntity<T> exchange(
URI url,
HttpMethod method,
HttpEntity<?> requestEntity,
ParameterizedTypeReference<T> responseType) throws RestClientException;
The given ParameterizedTypeReference is used to pass generic type information
|