RestTemplate 句柄 [image/jpg] 响应内容类型 java

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

抱歉,我是 java 网络开发的新手。

我有一些任务要通过 HTTP rest(GET 方法)从第 3 方公司获取用户个人资料图片。他们的 api 只能使用 header 参数上的 session id 访问,并且 api 将 return 一些字节 [] 数组看起来像 ’ÑÒBRSb¢ÂáTr²ñ#‚4“â3C等等

如何在 Rest 模板中处理内容类型为 image/jpg 的 Rest 响应?

我就这样努力

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;
}

此代码将return出错

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

如有任何建议或帮助,我们将不胜感激!

谢谢

更新

使用 stackoveflower 建议我可以设法解决这个问题。

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;
}

关于 HttpMessageConverter 的注意事项,而不是使用列表,我可以直接添加一个 ByteArrayHttpMessageConverter()

按我说的我猜你一定用对了messageconverter 我会这样做:

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;
}

可在此处找到更多信息: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

非常感谢,这个问题占用了我很多时间。现在,问题解决了。 以下:

@Configuration
@Slf4j
public class RestTemplateConfiguration implements ApplicationContextAware {

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    RestTemplate restTemplate = (RestTemplate) applicationContext.getBean("restTemplate");
    restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
    restTemplate.setUriTemplateHandler(new GetUriTemplateHandler());
}

}