如何将 RestTemplate 与 application/octet-stream 响应类型一起使用
Posted
技术标签:
【中文标题】如何将 RestTemplate 与 application/octet-stream 响应类型一起使用【英文标题】:How to use RestTemplate with application/octet-stream response type 【发布时间】:2016-12-21 15:56:00 【问题描述】:我正在使用ISBNdB 来获取有关书籍的信息。响应类型是应用程序/八位字节流。我得到的示例 json 响应如下所示
"index_searched" : "isbn",
"data" : [
"publisher_id" : "john_wiley_sons_inc",
"publisher_name" : "John Wiley & Sons, Inc",
"title_latin" : "Java programming interviews exposed",
"language" : "eng",
"summary" : "",
"physical_description_text" : "1 online resource (xvi, 368 pages) :",
"author_data" : [
"name" : "Markham, Noel",
"id" : "markham_noel"
,
"id" : "greg_milette",
"name" : "Greg Milette"
],
"title_long" : "Java programming interviews exposed",
"urls_text" : "",
"publisher_text" : "New York; John Wiley & Sons, Inc",
"book_id" : "java_programming_interviews_exposed",
"awards_text" : "; ",
"subject_ids" : [],
"isbn13" : "9781118722862",
"lcc_number" : "",
"title" : "Java programming interviews exposed",
"isbn10" : "1118722868",
"dewey_decimal" : "005.13/3",
"edition_info" : "; ",
"notes" : "\"Wrox programmer to programmer\"--Cover.; Acceso restringido a usuarios UCM = For UCM patrons only.",
"marc_enc_level" : "",
"dewey_normal" : "5.133"
]
我正在使用 Jackson 来转换此响应。我的 Pojo 如下所示
@JsonIgnoreProperties(ignoreUnknown = true)
public class value
private String index_searched;
// Another pojo in different file with ignore properties
private data[] dat;
public value()
public data[] getDat()
return dat;
public void setDat(data[] dat)
this.dat = dat;
public String getIndex_searched()
return index_searched;
public void setIndex_searched(String index_searched)
this.index_searched = index_searched;
当我尝试关注时
value book = restTemplate.getForObject(FINAL_URL, value.class);
我得到了这个异常
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.rocketrenga.mylibrary.domain.value] and content type [application/octet-stream]
但我可以将响应映射到字符串
String book = restTemplate.getForObject(FINAL_URL, String.class);
ObjectMapper mapper = new ObjectMapper();
value val = mapper.readValue(book, value.class);
System.out.println(val.getIndex_searched());
如何直接映射响应 POJO 而不是 String 并转换回 POJO
【问题讨论】:
到目前为止,您尝试将响应直接转换为 pojo 的方法是什么? 我已经说明了我在我的问题中尝试过的内容。我尝试的部分...... 问题出在服务器端。为什么要发送内容类型为application/octet-stream
的 JSON?
@AndyWilkinson。这就是我从 ISBNdb 收到的。我无法控制它。
呃。然后,您应该按照@jny 下面的建议进行操作,并使用基于 Jackson 的转换器配置您的 RestTemplate
,该转换器将 application/octet-stream
响应视为 JSON。
【参考方案1】:
您需要使用消息转换器配置 restTemplate。在您的配置中执行以下操作:
@Bean
public RestOperations restTemplate()
RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(
Arrays.asList(new MediaType[]MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
restTemplate.setMessageConverters(Arrays.asList(converter, new FormHttpMessageConverter()));
return restTemplate;
【讨论】:
【参考方案2】:我想更好的解决方案是添加另一个转换器,而不是修改当前的:
@Bean
public RestTemplate restTemplate()
final RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(jacksonSupportsMoreTypes());
return restTemplate;
private HttpMessageConverter jacksonSupportsMoreTypes() //eg. Gitlab returns JSON as plain text
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Arrays.asList(MediaType.parseMediaType("text/plain;charset=utf-8"), MediaType.APPLICATION_OCTET_STREAM));
return converter;
【讨论】:
以上是关于如何将 RestTemplate 与 application/octet-stream 响应类型一起使用的主要内容,如果未能解决你的问题,请参考以下文章
OAuth2RestTemplate 将字符集添加到内容类型标头中