RestTemplate在java中处理[image / jpg]响应内容类型

时间:2018-03-22 10:01:06

标签: java spring tapestry

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

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

如何在Rest模板中使用内容类型image / jpg处理休息响应?

我尽我所能

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

此代码将返回错误

  

org.springframework.web.client.RestClientException:无法解压缩   响应:没有为响应类型找到合适的HttpMessageConverter   [[B]和内容类型[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,而不是使用list,我可以直接添加ByteArrayHttpMessageConverter()

1 个答案:

答案 0 :(得分:1)

如上所述,我猜你必须使用正确的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-和此处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/ByteArrayHttpMessageConverter.html