2017-06-14 135 views
2

我需要調用返回一個API以下JSON格式,並將其轉換成Java對象:錯誤讀取從輸入流實體 - 利用傑克遜

GET /api/myimpl/?ids=5,12 

HTTP 200 OK 
Allow: GET, POST, HEAD, OPTIONS 
Content-Type: application/json 
Vary: Accept 

[ 
    { 
     "id": "A1", 
     "impl": "HeatSmart" 
    }, 
    { 
     "id": "B2", 
     "impl": "My String" 
    } 
] 

我有下面的類:

public class QueryStrings { 
    private String id; 
    private String impl; 

    public QueryStrings() { 
    } 

    public QueryStrings(String id, String impl) { 
     this.id = id; 
     this.impl = impl; 
    } 

    public String getId() { 
     return id; 
    } 

    public String getImpl() { 
     return impl; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public void setImpl(String impl) { 
     this.impl = impl; 
    } 
} 

在我的代碼,我做了以下內容:

client = ClientBuilder.newClient().register(JacksonFeature.class); 
Response clientResponse = client.target(myURL) 
     .queryParam("ids", "5,12") 
     .request() 
     .accept(MediaType.APPLICATION_JSON_TYPE) 
     .get(); 
QueryStrings jsonResponse = clientResponse.readEntity(QueryStrings.class); 

的clientResponse返回狀態OK。然而,當我試圖讓json進入我想要的對象時,最後一行返回錯誤:

javax.ws.rs.ProcessingException: Error reading entity from input stream. 

我的代碼中缺少什麼?

+1

使用GenericType我的理解,HTTP響應包含'QueryStrings'的陣列? –

回答

2

api回報QueryStrings陣列,這樣你就可以在readEntity

List<QueryStrings> results = response.readEntity(new GenericType<List<QueryStrings>>(){});