2013-12-08 151 views
0

我想反序列化來自Nutritionix API的JSON響應。當我嘗試反序列化對象時,我一直得到HTTP狀態500 - 請求處理失敗;嵌套異常是java.lang.ClassCastException:java.lang.String不能轉換爲java.util.Map錯誤。我非常困惑,不知道爲什麼這會導致事件發生,因爲看起來我得到了有效的JSON響應,但我不確定。反序列化問題

public class CallSearch { 

    String value; 

    public CallSearch(String search){ 
     value = search.replace(" ", "%20"); 
    } 
    public FullResponse search() throws ClientProtocolException, IOException{ 

     CloseableHttpClient client = HttpClients.createDefault(); 
     HttpGet getProducts = new HttpGet("https://api.nutritionix.com/v1_1/search/"+ value +"?results=0%3A6&cal_min=0&cal_max=50000&fields=item_name%2Citem_id&appId=ac23ceb3&appKey=e2b579c55b4857157fc6045d6296b532"); 
     CloseableHttpResponse productResponse = client.execute(getProducts); 
     String entityString = EntityUtils.toString(productResponse.getEntity()); 
     FullResponse test = new JSONDeserializer<FullResponse>().deserialize(entityString,FullResponse.class); 

     return test; 
    } 
} 

我創建了反序列化到類如下

public class FullResponse { 

    String total_hits; 
    String max_score; 
    List<Hits> hits; 

    public FullResponse(){ 

    } 

    public String getTotal_hits() { 
     return total_hits; 
    } 

    public void setTotal_hits(String total_hits) { 
     this.total_hits = total_hits; 
    } 

    public String getMax_score() { 
     return max_score; 
    } 

    public void setMax_score(String max_score) { 
     this.max_score = max_score; 
    } 

    public List<Hits> getHits() { 
     return hits; 
    } 

    public void setHits(List<Hits> hits) { 
     this.hits = hits; 
    } 

命中類,它是完整的響應

public class Hits { 

     String _index; 
     String _type; 
     String _id; 
     String _score; 
     List<Fields> fields; 

     public Hits(){ 

     } 

     public String get_index() { 
     return _index; 
    } 

    public void set_index(String _index) { 
     this._index = _index; 
    } 

    public String get_type() { 
     return _type; 
    } 

    public void set_type(String _type) { 
     this._type = _type; 
    } 

    public String get_id() { 
     return _id; 
    } 

    public void set_id(String _id) { 
     this._id = _id; 
    } 

    public String get_score() { 
     return _score; 
    } 

    public void set_score(String _score) { 
     this._score = _score; 
    } 

    public List<Fields> getFields() { 
     return fields; 
    } 

    public void setFields(List<Fields> fields) { 
     this.fields = fields; 
    } 
} 

最後字段類中的一個列表:

public class Fields { 

    String item_name; 

    public Fields(){ 

    } 

    public String getField(){ 
     return item_name; 
    } 
    public void setField(String name){ 
     item_name=name; 
    } 
} 

任何幫助我可能會出錯的地方將非常感激。由於

好吧,我所做的變化,這裏是JSON響應我得到

{ 
"total_hits":11025, 
"max_score":11.122117, 
"hits":[{ 
"_index":"nixproductionv13", 
"_type":"item", 
"_id":"513fceb375b8dbbc210000e4", 
"_score":11.122117, 
"fields":{"item_name":"Whole Milk - 1 tbsp"}}, 

{"_index":"nixproductionv13", 
"_type":"item", 
"_id":"513fceb375b8dbbc2100017b", 
"_score":10.7038355, 
"fields":{"item_name":"2% Milk - 1 cup"}}, 

{"_index":"nixproductionv13", 
"_type":"item", 
"_id":"513fceb375b8dbbc210000f3", 
"_score":10.7038355, 
"fields":{"item_name":"1% Milk - 1 cup"}}, 

{"_index":"nixproductionv13", 
"_type":"item", 
"_id":"513fceb375b8dbbc210000fb", 
"_score":10.689078, 
"fields":{"item_name":"Skim Milk - 1 cup"}}, 

{"_index":"nixproductionv13", 
"_type":"item", 
"_id":"513fceb375b8dbbc210000e3", 
"_score":10.65872, 
"fields":{"item_name":"Whole Milk - 1 fl oz"}}, 

{"_index":"nixproductionv13", 
"_type":"item", 
"_id":"513fceb375b8dbbc2100017a", 
"_score":10.392, 
"fields":{"item_name":"2% Milk - 1 quart"}}]} 

它現在導致一個嵌套的例外是java.lang.IllegalArgumentException異常:參數類型不匹配

org.springframework.web.util.NestedServletException:請求 處理失敗;嵌套異常是 java.lang.IllegalArgumentException:參數類型不匹配 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778 ) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter( FilterChainProxy.java:330) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java: 84) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) org.springframework.security.web .FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(的FilterChainProxy的.java:342) org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web .savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) org.springframework.security.web。的FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter( FilterChainProxy.java:342) org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 組織.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springfram ework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.security.web.FilterChainProxy。 doFilterInternal(FilterChainProxy.java:192) org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) org.springframework .web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

+0

發佈異常的完整堆棧跟蹤和JSON字符串。 –

+0

做一個'System.out.println(entityString)'併發布它打印的內容。 –

+0

你有不同的例外嗎?發佈它,如果你這樣做。 –

回答

3

這不會解決所有問題,但它將使我們在右邊TR ACK

String entityString = productResponse.getEntity().toString(); 

應該

String entityString = EntityUtils.toString(productResponse.getEntity()); 

EntityUtils.toString()狀態

的Javadoc閱讀一個實體的內容,並返回它作爲一個字符串。內容 使用來自實體的字符集(如果有的話)轉換,失敗 表示使用「ISO-8859-1」。

當你

productResponse.getEntity().toString(); 

要調用的Object類,它繼承ResponseEntityWrappertoString()方法。這只是返回String的值[email protected],它根本不是HTTP響應的主體。