elasticsearch - 无法为持久文档时看到的类型类的未知值写入 xcontent
问题描述
Getting the following error while updating phone
lang.IllegalArgumentException: cannot write xcontent for unknown value of type class com.company.Phone]
http://localhost/users/vfu1i3cB1FVgFWdzuLfj/update
{
"nu": "9823456344",
"cc": "91"
}
转换器看起来像这样
@WritingConverter
public static class ToMap implements Converter<Phone, Map<String, Object>> {
@Override
public Map<String, Object> convert(Phone source) {
LinkedHashMap<String, Object> target = new LinkedHashMap<>(8);
target.put(NAME, source.name);
target.put(NUMBER, source.number);
target.put(COUNTRY_CODE, source.countryCode);
return target;
}
}
@ReadingConverter
public static class ToPhone implements Converter<Map<String, Object>, Phone> {
@Override
public Phone convert(Map<String, Object> source) {
Phone phone = new Phone();
phone.setName((String)source.get(NAME));
phone.setNumber((String)source.get(NUMBER));
phone.setCountryCode((String)source.get(COUNTRY_CODE));
return phone;
}
}
解决方案
除了设置转换器之外,您还需要注册转换器实现:
@Bean
public ElasticsearchCustomConversions elasticsearchCustomConversions() {
return new ElasticsearchCustomConversions(
Arrays.asList(new ToPhone(), new ToMap())
);
}
推荐阅读
- apache - 如何解释 apache mod 重写规则?
- javascript - Google Apps Script Doesn't Work When Embedded. TypeError: Cannot call method "getActiveSheet" of null
- php - 将变量从 Php 下拉列表传递到 Ajax
- sql - Hive 根据日期范围汇总表中的数据
- angular - mat-datepicker 上的多种 dateInput 格式
- python - 在 Python 中的混合效应模型中指定交叉随机效应的符号
- mysql - 如何构建一个 MySQL 查询来提取不同的对,并限制类别可以匹配的次数?
- javascript - 如何处理数组元素之一为空的JS破坏赋值?
- python-3.x - 如何只计算不同的行?
- c# - 如何在 CreateDerivedCollection 方法中以多线程方式构建 ReactiveList