将客户端数据放入Java对象中.正在获取com.fasterxml.jackson.databind.exc.Misma



我有一个示例后端响应,如下所示:当我试图将这个响应映射到java对象时,我得到了以下错误。

com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从START_OBECT令牌中反序列化com.mc.membersphere.model.MemberSummaryLabel[]的实例

似乎是来自API的body标记的问题。它有一个对象数组。我需要帮助,如何在Java映射中处理这个body标记数组的值?

Backend API Response:
{
"body": [{
"pcp": "KASSAM, Far",
"er12M": "0",
"ipAdmits12M": "0",
"ipReAdmits12M": "0",
"rx12M": "0",
"pastMedicalHistory": " ",
"erCost12M": "0.0"
}
]
}

Java程序将Rest数据获取到Java对象中,如下所示。

import java.util.Collections;
import java.util.Properties;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.mc.membersphere.model.MemberSummaryLabel;
import com.mc.membersphere.utility.PropertyUtil;
public class TestRestclient implements CommandLineRunner{
public static void main(String[] args) {
SpringApplication.run(TestApi.class, args); }
private static Properties prop = PropertyUtil.getProperties();
@Override
public void run(String... args) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
String getMVPSummaryUrl = prop.getProperty("getmvpmembersummary.url");
String url = getMVPSummaryUrl+"/"+"CA";
ResponseEntity<MemberSummaryLabel[]> response = restTemplate.exchange(url, HttpMethod.GET,entity, MemberSummaryLabel[].class);
if(response.getStatusCode()== HttpStatus.OK) {
for(MemberSummaryLabel memberSummaryLabel : response.getBody())
{
System.out.println(memberSummaryLabel.pcp);
}
//System.out.println("Print response" + response);
}
else {
System.out.println("Error");
}
}
}

MemberSummaryLabel如下所示。

import com.fasterxml.jackson.annotation.JsonProperty;
public class MemberSummaryLabel {
@JsonProperty("pcp")
public String pcp;
@JsonProperty("er12M")
public Integer er12M;
@JsonProperty("ipAdmits12M")
public Integer ipAdmits12M;
@JsonProperty("ipReAdmits12M")
public Integer ipReAdmits12M;
@JsonProperty("rx12M")
public Integer rx12M;
@JsonProperty("pastMedicalHistory")
public String pastMedicalHistory;
@JsonProperty("erCost12M")
public Double erCost12M;
}

我明白了,这是映射的一个问题。您的响应在"body"中,body包含MemberSummaryLabel的列表。所以,你需要有一个以上提到的类,

public class Body{
@JsonProperty("body")
public List<MemberSummaryLabel> memberSummaryLabelList;
}

您的exchange方法应该返回NewClass

ResponseEntity<Body> response = restTemplate.exchange(url, HttpMethod.GET,entity, Body.class);

对于迭代使用,

for(MemberSummaryLabel memberSummaryLabel : response.getBody().getMemberSummaryLabelList()){
}

相关内容

最新更新