【问题标题】:Rest client data into Java object. Getting com.fasterxml.jackson.databind.exc.MismatchedInputException将客户端数据保存到 Java 对象中。获取 com.fasterxml.jackson.databind.exc.MismatchedInputException
【发布时间】:2025-11-24 02:20:03
【问题描述】:

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

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

【问题讨论】:

    标签: java spring-boot resttemplate


    【解决方案1】:

    我明白了,您的映射有问题。您的回复在“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()){
    }
    

    【讨论】:

    • 我明白你的意思。你说的对。一个小问题。我在迭代线上遇到了铸造错误。所以它试图投射到 MemberSummaryLabel 或什么?我有点困惑。
    • 你能添加堆栈跟踪吗?根据我的理解,你不应该得到强制转换异常。
    • 它给出编译错误并说 - 方法 getMemberSummaryLabelList() 未定义类型 ResponseEntity
    • 您是否添加了 gettersetter。我没有添加该代码。 :) 只需添加这些方法。如果您使用的是 lombork 则只需添加 gettersetter 注释。
    • 已经添加了getter setter。仍然给出同样的错误。 :(