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

标签 java spring-boot resttemplate

我有一个示例后端响应如下: 当我尝试将此响应映射到 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/

相关文章:

java - 在 oAuth2 资源服务器应用程序中使用@WithMockUser(与@SpringBootTest)

java - 禁用 Springboot RSocket 项目的 "default"Netty 端口

java - 如何在java spring中的列表中有效重复元素?

java - 使用 RestTemplate 和 Jackson 反序列化对 Java 的 JSON 响应

java - 在 Springboot 应用程序中使用 Rest Template 调用使用 @Async 注解的方法

java - mvn 集成测试期间没有运行 selenium 测试

connection - 如果应用程序崩溃,Java JRE 会关闭连接吗?

java - 尝试使用 RestTemplate 获取对象列表时响应正文为空

java - 为什么我的 ListView 适配器在新 View 上没有显示正确的布局?

java - 如何从文本中检索各种日期和时间值