使用 CustomEntityMapper 时读取时未填充 spring-data-elastic Id 字段 [英] spring-data-elastic Id field not populated on reads when using CustomEntityMapper
问题描述
弹性版本 - 1.7.6弹簧靴 - 1.3.5
Elasticversion - 1.7.6 springboot - 1.3.5
使用 spring-data-elasticsearch 我已经按照其他地方的建议创建了一个自定义 JSON 映射,以支持 Java8 的新日期时间字段.这工作正常 - 但由于 id 字段不再填充,因此会破坏从存储库中读取实体.
Using spring-data-elasticsearch I have created a custom JSON mapping as advised elsewhere in order to support Java8 new datetime fields. This works fine - but breaks reading entities from the repository as the id field no longer gets populated.
自定义配置:
@Bean
@Autowired
public ElasticsearchTemplate elasticsearchTemplate(Client client) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
return new ElasticsearchTemplate(client, new CustomEntityMapper(objectMapper));
}
public class CustomEntityMapper implements EntityMapper {
private ObjectMapper objectMapper;
public CustomEntityMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
}
@Override
public String mapToString(Object object) throws IOException {
return objectMapper.writeValueAsString(object);
}
@Override
public <T> T mapToObject(String source, Class<T> clazz) throws IOException {
return objectMapper.readValue(source, clazz);
}
}
示例实体:
@Document(indexName = "scanner", type = "Entry")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Entry {
@Id
private String id;
@Field(type= FieldType.String)
private String path;
@Field(type = FieldType.Date, format = DateFormat.date_time )
private OffsetDateTime created;
}
注意 - 当我删除 CustomEntityMapper 时,会返回 id 字段.我已经追踪了spring-data-elasticsearch的代码,并确定它无法从 DefaultResultMapper.setPersistentId 中的弹性响应解析 Id 字段,因为mappingContext 为空.
Note - that when I remove the CustomEntityMapper the id field is returned. I have traced the spring-data-elasticsearch code, and identified that it fails to resolve the Id field from the elastic response in DefaultResultMapper.setPersistentId since the mappingContext is null.
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
PersistentProperty<ElasticsearchPersistentProperty> idProperty = mappingContext.getPersistentEntity(clazz).getIdProperty();
// Only deal with String because ES generated Ids are strings !
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
Method setter = idProperty.getSetter();
if (setter != null) {
try {
setter.invoke(result, id);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}
有人遇到过这个问题吗?如何在不破坏 Id 分辨率的情况下支持 CustomEntityMapper?
Has anyone experienced this issue? How can I support a CustomEntityMapper without breaking the Id resolution?
推荐答案
升级到 spring boot 1.4.1-RELEASE 解决了问题
upgrading to spring boot 1.4.1-RELEASE resolved the issue
这篇关于使用 CustomEntityMapper 时读取时未填充 spring-data-elastic Id 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!