2
votes

Elasticversion - 1.7.6 springboot - 1.3.5

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.

CustomConfig:

@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);
    }
}

Sample Entity :

@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;

}

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();
                    }
                }
            }
        }
    }

Has anyone experienced this issue? How can I support a CustomEntityMapper without breaking the Id resolution?

1
Looks like it has been fixed in later releases jira.spring.io/browse/DATAES-141trorbyte

1 Answers

0
votes

upgrading to spring boot 1.4.1-RELEASE resolved the issue