2016-11-25 198 views
0

我有API返回JSON格式如下:春JSON映射到Java POJO

[ 
    { "shrtName": "abc", "validFrom": "2016-10-23", "name": "aaa", "version": 1 }, 
    { "shrtName": "def", "validFrom": "2016-11-20", "name": "bbb", "version": 1 }, 
    { "shrtName": "ghi", "validFrom": "2016-11-22", "name": "ccc", "version": 1 } 
] 

我有這樣的代碼讀取API並將它作爲一個字符串。但是我想讀這個API並將其映射到Java POJO類中。

public String downloadAPI(){ 
     RestTemplate restTemplate = new RestTemplate(); 
     HttpHeaders headers = new HttpHeaders(); 
     headers.set("API-Key", "4444444-3333-2222-1111-88888888"); 
     HttpEntity<?> requestEntity = new HttpEntity<Object>(headers); 
     String URL = "https://aaaaaaa.io/api/v1/aaaaaaaaa?date=2015-04-04; 
     restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); 
     ResponseEntity<String> response = restTemplate.exchange(URL, HttpMethod.GET, requestEntity, String.class); 
     return response.getBody(); 
} 

我的問題:
1)POJO的格式?
2)改變我的方法(返回類型POJO而不是字符串)

回答

3

你JSON是一個數組,這就是爲什麼[]

創建POJO

public class MyPOJO { 
    private String shrtName; 
    private Date validFrom; 
    private String name; 
    private int version;  
} 

刪除消息變換和重構restTemplate交換方法

ResponseEntity<MyPOJO[].class> response = restTemplate.exchange(URL, HttpMethod.GET, requestEntity, MyPOJO[].class); 

這是通用功能我使用GET請求

public <T> T getRequestAndCheckStatus(final String url, final Class<T> returnTypeClass, 
              final List<MediaType> mediaTypes, 
              final Map<String, String> headerParams, 
              final Map<String, Object> queryParams) throws Exception { 

     final HttpHeaders headers = new HttpHeaders(); 
     headers.setAccept(mediaTypes); 
     setHeaderParamsIfExists(headers, headerParams); 
     final HttpEntity<String> requestEntity = new HttpEntity<>(headers); 

     UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(url); 
     setQueryParamsIfExists(uriBuilder, queryParams); 

     final ResponseEntity<T> entity = restTemplate 
       .exchange(getUrl(uriBuilder), 
          HttpMethod.GET, 
          requestEntity, 
          returnTypeClass); 

     Assert.assertEquals(HttpStatus.OK, entity.getStatusCode()); 
     return entity.getBody(); 
    } 

    private void setHeaderParamsIfExists(HttpHeaders headers, Map<String, String> headerParams) { 
     if(headerParams != null && !headerParams.isEmpty()) 
      headerParams.entrySet() 
        .forEach(entry -> headers.set(entry.getKey(), entry.getValue())); 
    } 

    private void setQueryParamsIfExists(UriComponentsBuilder uriBuilder, Map<String, Object> queryParams) { 
     if(queryParams != null && !queryParams.isEmpty()) 
      queryParams.entrySet() 
        .forEach(entry -> uriBuilder.queryParam(entry.getKey(), entry.getValue())); 
    } 

    private URI getUrl(UriComponentsBuilder uriBuilder) { 
     return uriBuilder.build().encode().toUri(); 
    } 

在你的情況,你會說它是由

getRequestAndCheckStatus("https://aaaaaaa.io/api/v1/aaaaaaaaa", MyPOJO[].class,      
          Collections.singletonList(MediaType.APPLICATION_JSON_UTF8), 
          new HashMap<String, String>(){{ put("API-Key", "4444444-3333-2222-1111-88888888"); }}), 
          new HashMap<String, Object>(){{ put("Date", "2015-04-04"); }}); 
  • Additionaly,對於日期,我建議使用長,然後在其控制器來解析日期。我看到你使用https協議,你配置了證書嗎?
+0

開頭「很好的解決方案,它的工作原理,感謝您分享您的通用功能。 –

0

創建一個pojo與這些atrributes和使用傑克森從json字符串轉換爲您的pojo。

public class MapClass { 

private String shrtName; 
private Date validFrom; 
private String name; 
private int version; 

} 
+0

哪些屬性?請你能寫一個代碼嗎?我嘗試過沒有理由... –

+0

public class MappedClass { private String shrtName; 私人日期validFrom; 私人字符串名稱; private int version; } – septum

+0

它不工作,因爲JSON以「[」 –