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

标签 java spring tapestry

抱歉,我是 Java Web 开发新手。

我有一些任务通过 HTTP Rest(GET 方法)从第 3 方公司获取用户个人资料图片。他们的 api 只能使用 header 参数上的 session id 来访问,并且 api 将返回一些 byte[] 数组,看起来像 'ÑÒ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;
}

此代码将返回错误

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-在这里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

关于java - RestTemplate在java中处理[image/jpg]响应内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49425622/

相关文章:

Atom DateFormat 的 Java 解析器和格式化程序

spring - 链接 Spring View 解析器

java - 部署 CXF Restful WebServices 时如何避免与 Spring 相关的警告

java - 哪种 Java Web 框架最适合 Web 设计人员?

java - 如何检索此 fragment 中的歌曲列表?

java - o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00917: missing comma

java - 如何通过JMX获取James Server中注册的用户列表

java - 在处理大数据时如何确保您的代码按预期工作?

java - Tapestry 500 错误 : EnvironmentImpl. stackFor(EnvironmentImpl.java:47) 可能不再被调用

java - Tapestry 5 : URL Re-writing : Pass parameters to transformPageRenderLink method