Spring RestClient与Jackson嵌套JSON处理:仅提取data数组问题
我有一个Rest客户端,调用接口后会返回JSON格式的数据,现在希望仅提取其中的data[]数组。返回的JSON结构如下:
{ "responseStatus": "SUCCESS", "responseDetails": { "limit": 1000, "offset": 0, "size": 2, "total": 2 }, "data": [ { "id": "00P000000001M01", "name__v": "Foo", "status__v": [ "active__v" ], "abbreviation__c": "F170053", "internal_name__c": "Foo", "therapeutic_area__c": [ "neurology__c" ], "external_id__v": "84", "generic_name__c": "Foo", "scientific_name__c": null }, { "id": "00P000000001N01", "name__v": "Bar", "status__v": [ "active__v" ], "abbreviation__c": "B333334", "internal_name__c": "Bar", "therapeutic_area__c": [ "bone_muscle_joint__c" ], "external_id__v": "101", "generic_name__c": "Bar", "scientific_name__c": null } ] }
由于其他接口返回的data[]中字段会不同,我希望将每种类型映射到对应的POJO,因此使用了@JsonProperty和@JsonIgnoreProperties注解,定义了继承自VBase的Product类:
@JsonIgnoreProperties(ignoreUnknown = true) public class Product extends VBase{ private String fNumber; private String genericName; private String scientificName; private String therapeuticArea; public String getFNumber() { return fNumber; } @JsonProperty("abbreviation__c") private void unpackFNumber(Map<String,Object> abbreviation__c){ fNumber = ((Map<String,Object>)abbreviation__c.get("data")).get("abbreviation__c").toString(); } public void setLyNumber(String fNumber) { this.fNumber = fNumber; } public String getGenericName() { return genericName; } @JsonProperty("generic_name__c") private void unpackGenericName(Map<String,Object> generic_name__c){ genericName = ((Map<String,Object>)generic_name__c.get("data")).get("generic_name__c").toString(); } public String getScientificName() { return scientificName; } @JsonProperty("scientific_name__c") private void unpackScientificName(Map<String,Object>sName){ scientificName = ((Map<String,Object>)sName.get("data")).get("scientific_name__c").toString(); } public String getTherapeuticArea() { return therapeuticArea; } @JsonProperty("therapeutic_area__c") private void unpackTheraputicArea(Map<String,Object>tArea){ therapeuticArea=((Map<String,Object>)tArea.get("data")).get("therapeutic_area__c").toString(); } }
我尝试了多种方式想直接获取data[]:
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers); RestTemplate restTemplate = new RestTemplate(clientHttpReq); ResponseEntity<Product[]> response = restTemplate.exchange(url, HttpMethod.GET, request,Product[].class); //ResponseEntity<Product[]> response = restTemplate.postForEntity(url,request,Product[].class ,parmMap);
我一直想避免创建包含Data[]字段的ResponseDetail POJO(虽然这种方式可行),但因为不同接口返回的data[]字段不同,我想知道能不能用一个包含List的通用包装类来表示data[],而且所有POJO都继承自VBase?
目前遇到的异常如下:
Exception in thread "main" org.springframework.web.client.RestClientException: Error while extracting response for type [class [Lcom.lilly.models.Product;] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of
com.lilly.models.Product[]out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance ofcom.lilly.models.Product[]out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]
内容的提问来源于stack exchange,提问作者jebrick

