Service Provider:
@RequestMapping(path = "...")
public MessageBox<Map<String, Object>> testReceive(...) {...}
The request type, interface address, and internal logic of the interface are not important. What is important is that the interface returns generic data.MessageBox<Map<String, Object>>
Service caller:
@PostMapping("...")
public void dealWithGenericData(...) {
...
String url = ...;
HttpEntity<String> fromEntity = ...;
// Process generic data ⭐
ParameterizedTypeReference<MessageBox<Map<String, Object>>> typeReference = new ParameterizedTypeReference<MessageBox<Map<String, Object>>>() {};
// Send a request
ResponseEntity<MessageBox<Map<String, Object>>> responseEntity = restTemplate.exchange(url, HttpMethod.POST, fromEntity, typeReference);
// Process response information
MessageBox<Map<String, Object>> body = responseEntity.getBody();
log.info(String.valueOf(body));
}
Related source code
/**
* Execute the HTTP method to the given URI template, writing the given
* request entity to the request, and returns the response as {@link ResponseEntity}.
* The given {@link ParameterizedTypeReference} is used to pass generic type information:
* <pre class="code">
* ParameterizedTypeReference<List<MyBean>> myBean = new ParameterizedTypeReference<List<MyBean>>() {};
* ResponseEntity<List<MyBean>> response = ("",, null, myBean);
* </pre>
* @param url the URL
* @param method the HTTP method (GET, POST, etc)
* @param requestEntity the entity (headers and/or body) to write to the request
* (may be {@code null})
* @param responseType the type of the return value
* @return the response as entity
* @since 3.2
*/
<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