gpt4 book ai didi

java - 将客户端数据放入 Java 对象中。获取 com.fasterxml.jackson.databind.exc.MismatchedInputException

转载 作者:行者123 更新时间:2023-12-02 02:02:08 29 4
gpt4 key购买 nike

我有一个示例后端响应如下:当我尝试将此响应映射到 java 对象时,出现以下错误。

com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_OBJECT token 中反序列化 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"

}
]
}

将Rest数据获取到Java对象的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()){
}

关于java - 将客户端数据放入 Java 对象中。获取 com.fasterxml.jackson.databind.exc.MismatchedInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51414379/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com