如何使用 Java High Level Rest Client API 映射

我正在使用 ES 7.2。下面是来自 ES 的示例查询结果。它有两个字段 STATUS 和 SERVICE_ID:


{{

  "took" : 4,

  "timed_out" : false,

  "_shards" : {

    "total" : 1,

    "successful" : 1,

    "skipped" : 0,

    "failed" : 0

  },

  "hits" : {

    "total" : {

      "value" : 106,

      "relation" : "eq"

    },

    "max_score" : 1.0,

    "hits" : [

      {

        "_index" : "myindex",

        "_type" : "_doc",

        "_id" : "ENXDrWsBF759w7WDGxK4",

        "_score" : 1.0,

        "_source" : {

          "STATUS" : "10",

          "SERVICE_ID" : "916"

        }

      },

      {

        "_index" : "myindex",

        "_type" : "_doc",

        "_id" : "EdXDrWsBF759w7WDGxK4",

        "_score" : 1.0,

        "_source" : {

          "STATUS" : "10",

          "SERVICE_ID" : "916"

        }

      }

    ]

  }

}


我想将它映射到下面的 Java Bean“MyBean”:


public class MyBean {


  String SERVICE_ID;

  String STATUS;


public String getSERVICE_ID() {

    return SERVICE_ID;

}

public void setSERVICE_ID(String sERVICE_ID) {

    SERVICE_ID = sERVICE_ID;

}

public String getSTATUS() {

    return STATUS;

}

public void setSTATUS(String sTATUS) {

    STATUS = sTATUS;

}

}

我已经使用 ObjectMapper 的 jackson 库尝试了下面的映射,但它有以下异常:


        try {

                    SearchResponse searchResponse1 = client.search(searchRequest, RequestOptions.DEFAULT);

            ObjectMapper mapper = new ObjectMapper();

            java.util.List<MyBean> lst = new ArrayList<MyBean>();

            for(SearchHit hit : searchResponse1.getHits().getHits()) {

//here below, i have the exception

                MyBean s = mapper.readValue(hit.getSourceAsString(), MyBean.class);

                lst.add(s);

            } 

以下是例外情况:


com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "STATUS" (class MyBean), not marked as ignorable (2 known properties: "status",  "service_ID")

 at [Source: (String)"{"STATUS":"11"}"; line: 1, column: 12] (through reference chain: MyBean["STATUS"])

你有什么主意吗?


SMILET
浏览 121回答 1
1回答

慕姐4208626

这个怎么样?public class MyBean {&nbsp; &nbsp; private String status;&nbsp; &nbsp; private String name;&nbsp;&nbsp; &nbsp; @JsonProperty("SERVICE_ID")&nbsp; &nbsp; public void setServiceId(String serviceId) {&nbsp; &nbsp; &nbsp; &nbsp; this.serviceId = serviceId;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; @JsonProperty("SERVICE_ID")&nbsp; &nbsp; public String getServiceId() {&nbsp; &nbsp; &nbsp; &nbsp; return serviceId;&nbsp; &nbsp; }&nbsp; &nbsp; @JsonProperty("STATUS")&nbsp; &nbsp; public void setStatus(String status) {&nbsp; &nbsp; &nbsp; &nbsp; this.status = status;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; @JsonProperty("STATUS")&nbsp; &nbsp; public String getStatus() {&nbsp; &nbsp; &nbsp; &nbsp; return status;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java