Java中的RestTemplate句柄[image/jpg]响应内容类型

8qgya5xd  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(269)

对不起,我是java网页开发新手。
我得到了一些任务,以获取用户配置文件图片从第三方公司通过HTTP休息(GET方法)。他们的API只能访问使用会话ID的头部参数和API将返回一些字节[]数组看起来像’ÑÒBRSb¢ÂáTr²ñ#‚4“â3C等。

    • 如何处理休息模板中内容类型为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:无法提取响应:找不到适合响应类型[[B]和内容类型[image/jpg]的HttpMessageConverter
任何建议或帮助将不胜感激!
谢谢

    • 更新**

使用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(),而不是使用列表

krcsximq

krcsximq1#

如前所述,我猜您必须使用正确的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;
}

更多信息请访问:www.example.com,此处为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.htmlhttps://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

wh6knrhe

wh6knrhe2#

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

@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());
}

}

相关问题