繁体   English   中英

使用 resttemplate 从 spring-boot 中的 Graph API 获取图像时出错

[英]Error in getting image from Graph API in spring-boot with resttemplate

我正在使用图表 api:

GET /users/{id | userPrincipalName}/photo/$value

使用我的访问令牌获取特定用户的个人资料照片。 在 postman 中,我可以使用上面的 get 调用看到图像。 在我的 spring-boot 应用程序中,我使用如下:

final ResponseEntity<Object> profilePicture = restTemplate.exchange(graphUrl, HttpMethod.GET, new HttpEntity<>((header)), new ParameterizedTypeReference<>() {});

我收到以下错误:

Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.Object] and content type [image/jpeg]

我已经将 RestTemplate 定义为:

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

有人可以帮我解决这个问题吗?

您需要将适当的MessageConverter添加到您的RestTemplate

就像是:

 RestTemplate restTemplate = new RestTemplate();
 restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
 
 ResponseEntity<byte[]> response = restTemplate.exchange(graphUrl, 
 HttpMethod.GET, new HttpEntity<>((header)), byte[].class);

您可以在此处阅读有关此主题的更多信息: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/HttpMessageConverter.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM