2016-08-15 40 views
0

我正在使用ISBNdB來獲取有關該書的信息。響應類型是application/octet-stream。我得到的樣本json響應看起來如下如何使用帶應用程序/八位字節流響應類型的RestTemplate

{ 
"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()); 
響應

如何去映射響應e直接POJO而不是字符串並轉換回POJO

+0

什麼的走近你試過到目前爲止直接轉換響應成pojo? –

+0

我已經說明了我的問題。這部分當我試圖...... – Rengas

+0

這個問題真的在服務器端。你爲什麼要發送一個內容類型爲「application/octet-stream」的JSON? –

回答

3

您需要爲消息轉換器配置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; 
    } 
3

我想更好的解決方法就是增加一個轉換器,不修改當前的:

@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; 
} 
相關問題