Home > OS >  Error in getting image from Graph API in spring-boot with resttemplate
Error in getting image from Graph API in spring-boot with resttemplate

Time:09-30

I am using the graph 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. In my spring-boot application I am using like below:

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:

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

Could someone please help me with this?

CodePudding user response:

You need to add an appropriate MessageConverter to your 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

  • Related