javaspringtapestry

RestTemplate handle [image/jpg] response content type in java


Sorry, i am newbie on java web development.

I got some task to fetch user profile picture from 3rd party company via HTTP rest(GET method). Their api only can be accessed using session id on the header parameter and the api will return some byte[] array looks like ’ÑÒBRSb¢ÂáTr²ñ#‚4“â3C etc.

How to handle rest response with content type image/jpg in Rest Template?

I do my best like this

private RestTemplate restTemplate;

public byte[] getProfilePic(){
  String canonicalPath = "http://dockertest/bankingapp/customer/profpicFile";
  String sessionId= "MTQ4NzE5Mz...etc";

  HttpEntity<byte[]> request = new HttpEntity<byte[]>(null, getHeaders(true, "GET", null, canonicalPath, sessionId));
  //getHeaders() will return HttpHeaders with those parameter

  ResponseEntity<byte[]> response = null;
  try {
    response = this.restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class);
  } catch( HttpServerErrorException hse ){
    throw hse;
  }
  return response;
}

This code will return an error

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [[B] and content type [image/jpg]

Any suggestion or help will be appreciated!

Thank you

Update

Using stackoveflower suggestions i can manage to solve this.

private RestTemplate restTemplate;

public byte[] getProfilePic(){
  String canonicalPath = "/mobile/customer/profpicFile";
  String sessionId= "MTQ4NzE5Mz...etc";

  HttpEntity<byte[]> request = new HttpEntity<byte[]>(null, getHeaders(true, "GET", null, canonicalPath, sessionId));
  //getHeaders() will return HttpHeaders with those parameter

  ResponseEntity<byte[]> response = null;
  try {
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    response = this.restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class).getBody();
    return response;
  } catch( HttpServerErrorException hse ){
    throw hse;
  }
  return null;
}

Note about HttpMessageConverter, instead using list, i can directly add a ByteArrayHttpMessageConverter()


Solution

  • As said I guess you must use the right messageconverter I would do in this way:

    private RestTemplate restTemplate;
    
    public byte[] getProfilePic(){
      String canonicalPath = "http://dockertest/bankingapp/customer/profpicFile";
      String sessionId= "MTQ4NzE5Mz...etc";
      List<HttpMessageConverter> converters = new ArrayList<>(1);
      converters.add(new ByteArrayHttpMessageConverter());
      restTemplate.setMessageConverters(converters);
      HttpEntity<byte[]> request = new HttpEntity<byte[]>(null, getHeaders(true, "GET", null, canonicalPath, sessionId));
      //getHeaders() will return HttpHeaders with those parameter
    
      ResponseEntity<byte[]> response = null;
      try {
        response = this.restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class);
      } catch( HttpServerErrorException hse ){
        throw hse;
      }
      return response;
    }
    

    More information can be found here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#setMessageConverters-java.util.List- and here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/HttpMessageConverter.html and here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/ByteArrayHttpMessageConverter.html