2013-12-08 5 views
1

API를 사용하여 영양 정보를 가져 오는 Spring MVC 프로젝트에 문제가 있습니다. java.lang.IllegalArgumentException : 인수 유형 불일치가 중첩 예외를 계속 가져옵니다. 아래에 게시 된 JSON 응답 및 deserialization 프로세스에 대해 만든 클래스입니다.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; 
} 
} 

goi 잘못하면 거대한 도움이 될 것입니다. 여기

내가 잘 flexjson을 모르는 요청

{ 
"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

에서 JSON이지만, 자바 String 필드에 JSON 숫자 필드로 변환 좋아하지 않는 것처럼 보인다. 이 오류는 예를 들어, 일이 자바 String 필드는 유형의 필드에 해당 JSON 숫자 필드가 모든 String 필드를 변경하는 경우

String _score; 

에이 JSON 번호

"_score":10.7038355 

를 역 직렬화 할 때 Double, 코드가 작동합니다.

또한 fields JSON 객체

"fields":{"item_name":"Whole Milk - 1 tbsp"}}, 

는 JSON 객체 아니라 JSON 배열되어 있습니다. 그러므로 당신은

Fields fields; 

List<Fields> fields; 

을 변경하고 적절하게 또한 getter와 setter를 변경해야합니다.


또는 Gson과 같은 다른 JSON 구문 분석 라이브러리를 사용하여 이러한 표준 유형 변환을 올바르게 지원할 수 있습니다.

+0

여전히 비 직렬화로 인해 제품 이름이 null 인 이유는 무엇입니까? – user3080107

+1

@ user3080107 예, 필드 이름과 일치하는 일반 getter 및 setter 이름을 사용해야합니다. 예를 들어,'setItem_name'과'getItem_name'. 또한 Java 명명 규칙에서는 변수 이름에'_'을 사용하지 않는 것이 좋습니다. 많은 라이브러리가 작동하는 [Java Bean convention] (http://en.wikipedia.org/wiki/JavaBeans)을 확인하십시오. –

+0

명명 규칙의 이유는 JSON 응답에서 제공 한 것이기 때문에 그 이름을 사용했다면 JSON 응답을 serialize 할 수 없다는 것입니다. – user3080107