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

时间:2018-07-19 04:31:15

标签: java spring-boot resttemplate

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

             }
          ]
      }

将剩余数据放入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;
   }

1 个答案:

答案 0 :(得分:2)

我知道,这与您的映射有关。您的回复在“正文”中,并且 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()){
}