我正在使用 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"])你有什么主意吗?
1 回答
慕姐4208626
TA贡献1852条经验 获得超7个赞
这个怎么样?
public class MyBean {
private String status;
private String name;
@JsonProperty("SERVICE_ID")
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
@JsonProperty("SERVICE_ID")
public String getServiceId() {
return serviceId;
}
@JsonProperty("STATUS")
public void setStatus(String status) {
this.status = status;
}
@JsonProperty("STATUS")
public String getStatus() {
return status;
}
}
添加回答
举报
0/150
提交
取消