我正在尝试从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;
}
Hits Class是完整响应中的列表
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;
}
}
最后是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:Request 处理失败;嵌套异常是 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) org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) org.springframework.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 :(得分:3)
这不会解决所有问题,但会让我们走上正轨
String entityString = productResponse.getEntity().toString();
应该是
String entityString = EntityUtils.toString(productResponse.getEntity());
EntityUtils.toString()
个州的javadoc
读取实体的内容并将其作为String返回。内容 使用实体中的字符集(如果有)转换,失败 那就是“ISO-8859-1”。
当你这样做时
productResponse.getEntity().toString();
您正在调用toString()
类的Object
方法,ResponseEntityWrapper
继承该方法。这只会返回String
值org.apache.http.impl.execchain.ResponseEntityWrapper@8f6b087
,它不是HTTP响应的主体。