抱歉,我是 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/