我的Spring MVC项目遇到了问题,该项目涉及使用API来提取营养成分。我继续得到一个嵌套异常是java.lang.IllegalArgumentException:参数类型不匹配。下面发布的是JSON响应和我为反序列化过程创建的类。
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=API_KEY");
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"}}]}
答案 0 :(得分:0)
我不太了解flexjson
,但似乎不喜欢从JSON数字字段转换为Java String
字段。例如,在尝试反序列化此JSON编号时会发生错误
"_score":10.7038355
进入此Java String
字段
String _score;
如果您将具有相应JSON数字字段的所有String
字段更改为Double
类型的字段,则您的代码将有效。
另请注意fields
JSON对象
"fields":{"item_name":"Whole Milk - 1 tbsp"}},
是一个JSON对象,而不是JSON数组。因此,您需要更改
List<Fields> fields;
到
Fields fields;
并适当地改变getter和setter。
或者,您可以使用不同的JSON解析库,如Gson,它可以很好地支持这些标准类型转换。