我们有一个rest服务,它在type的映射内返回一个字节数组。如果我使用不带泛型的Map接收响应,则字节数组数据将转换为String。是否可以仅从服务器发送字节数据,如果可以,如何使用RestTemplate从客户端检索该数据?
ResponseEntity<Map<String, byte[]>> result result = restTemplate.exchange("http://localhost:8085/api/fetchContent?Id=" + contentId+"&userName=trump", HttpMethod.GET, entity, Map.class, params);
上面的代码将给出编译问题,因为返回类型是一个映射。
答案 0 :(得分:1)
使用ParameterizedTypeReference<T>
:
ParameterizedTypeReference<Map<String, byte[]>> responseType =
new ParameterizedTypeReference<Map<String, byte[]>>() {};
ResponseEntity<Map<String, byte[]>> responseEntity =
restTemplate.exchange("http://example.org", HttpMethod.GET, entity, responseType);