简体   繁体   English

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

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

I am using the graph api:我正在使用图表 api:

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

to get the particular user's profile photo with my access token.使用我的访问令牌获取特定用户的个人资料照片。 In postman I am able to see the image using the above get call.在 postman 中,我可以使用上面的 get 调用看到图像。 In my spring-boot application I am using like below:在我的 spring-boot 应用程序中,我使用如下:

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

I am getting below error:我收到以下错误:

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

I have defined RestTemplate like:我已经将 RestTemplate 定义为:

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

Could someone please help me with this?有人可以帮我解决这个问题吗?

You need to add an appropriate MessageConverter to your RestTemplate .您需要将适当的MessageConverter添加到您的RestTemplate

Something like:就像是:

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

You can read more on this subject here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/HttpMessageConverter.html您可以在此处阅读有关此主题的更多信息: 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