如何从restTemplate交换方法获取通用映射作为响应?

时间:2019-01-10 09:21:33

标签: rest spring-boot resttemplate

我们有一个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);

上面的代码将给出编译问题,因为返回类型是一个映射。

1 个答案:

答案 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);