当前位置: 首页 > 知识库问答 >
问题:

Spring数据弹性搜索4.1

罗昕
2023-03-14

升级到Spring boot 2.3和Spring data elasticsearch 4.0.9后的问题。我有这样的文档:

public class Entity implements Serializable {
    @Id private String id;
    private URL url;
    ...
}

这在spring data 3.0中与Jackson配合得很好,但升级到4.0后,Jackson不再可用,现在我收到了一个来自spring的实例化异常,无法实例化URL对象。

例外情况:

org.springframework.data.mapping.model.MappingInstantiationException: Failed to instantiate java.net.URL using constructor NO_CONSTRUCTOR with arguments 
    at org.springframework.data.mapping.model.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:65)
    at org.springframework.data.mapping.model.ClassGeneratingEntityInstantiator.createInstance(ClassGeneratingEntityInstantiator.java:87)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readEntity(MappingElasticsearchConverter.java:178)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readMapValue(MappingElasticsearchConverter.java:375)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readValue(MappingElasticsearchConverter.java:296)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter$ElasticsearchPropertyValueProvider.getPropertyValue(MappingElasticsearchConverter.java:915)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readProperties(MappingElasticsearchConverter.java:253)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.readEntity(MappingElasticsearchConverter.java:185)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.read(MappingElasticsearchConverter.java:165)
    at org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter.read(MappingElasticsearchConverter.java:74)
    at org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate$ReadDocumentCallback.doWith(AbstractElasticsearchTemplate.java:602)
    at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.get(ElasticsearchRestTemplate.java:166)
    at org.springframework.data.elasticsearch.repository.support.AbstractElasticsearchRepository.findById(AbstractElasticsearchRepository.java:118)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72)
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382)
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:550)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy176.findById(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy176.findById(Unknown Source)
    at com.callfire.starters.elasticsearch.starter.store.AbstractGenericElasticSearchMapStore.load(AbstractGenericElasticSearchMapStore.java:153)
    at com.callfire.starters.elasticsearch.starter.store.AbstractGenericElasticSearchMapStore.load(AbstractGenericElasticSearchMapStore.java:50)
    at com.hazelcast.map.impl.MapStoreWrapper.load(MapStoreWrapper.java:165)
    at com.hazelcast.map.impl.mapstore.writebehind.WriteBehindStore.load(WriteBehindStore.java:206)
    at com.hazelcast.map.impl.mapstore.writebehind.WriteBehindStore.load(WriteBehindStore.java:56)
    at com.hazelcast.map.impl.recordstore.DefaultRecordStore.putIfAbsent(DefaultRecordStore.java:1004)
    at com.hazelcast.map.impl.operation.PutIfAbsentOperation.run(PutIfAbsentOperation.java:36)
    at com.hazelcast.spi.Operation.call(Operation.java:170)
    at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.call(OperationRunnerImpl.java:210)
    at com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:199)
    at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:147)
    at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.process(OperationThread.java:125)
    at com.hazelcast.spi.impl.operationexecutor.impl.OperationThread.run(OperationThread.java:110)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.net.URL]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.net.URL.<init>()
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:146)
    at org.springframework.data.mapping.model.ReflectionEntityInstantiator.createInstance(ReflectionEntityInstantiator.java:62)
    ... 54 common frames omitted
Caused by: java.lang.NoSuchMethodException: java.net.URL.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.getDeclaredConstructor(Class.java:2178)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:139)
    ... 55 common frames omitted

任何关于解决方案的想法都将受到赞赏。

共有3个答案

经佐
2023-03-14

长话短说。在检查了旧的现有索引URL上的数据后,它被存储为String,可能是Jackson HAL模块。我能够通过添加两个转换器来保留行为。

    @ReadingConverter
public static class StringToUrlConverter implements Converter<String, URL> {
    @SneakyThrows
    @Override
    public URL convert(String url) {
        return new URL(url);
    }
}

@WritingConverter
public static class UrlToStringConverter implements Converter<URL, String> {
    @Override
    public String convert(URL url) {
        return url.toString();
    }
}
金坚
2023-03-14

从索引中的数据来看,URL似乎存储为一个嵌套对象,其属性为协议、主机、端口、文件、权限、路径、处理程序(本身是一个对象)。

在读取Spring Data时,Elasticsearch尝试创建类URL的对象,以便在对象上设置这些属性。但是URL类没有构造函数来执行此操作。

要读取它,您需要定义一个自定义转换器,它可以读取和写入Document对象的URL(基本上是Map

叶煌
2023-03-14

这是因为您可能没有任何以URL作为参数进行实例化的构造函数。

在您的情况下,构造函数应该是这样的。

public Entity(URL url, ** **) {
    this.url = url;
    **;
    **;
}

此外,您可以使用Lombok通过@NoArgsConstructor、@AllArgsConstructor、@Data等注释获取构造函数功能,只需将其添加到您的模型类中即可工作。

 类似资料:
  • 我有一个设计糟糕的文档结构: 我想从上面的文档中检索标题、Fach和代码。 多谢了。

  • 我正在使用spring数据elasticsearch来执行CRUD操作。 我有一个扩展Elasticsearch chRepository的自定义存储库。 最终,ElasticsearchRepository扩展了CrudRepository,这意味着可以更新现有记录。 问题是,你是如何做到这一点的?我还没有找到一个名为“update()”的方法 我认为做以下事情会有用(代码从https://gi

  • 我需要运行以下查询: 但我不能用spring data elasticsearch轻松运行这个。 有什么办法吗 spring data elasticsearch是否很好地支持所有elasticsearch查询DSL

  • 我正在尝试在多个集群(而不是节点)中插入记录。我正在使用spring-data-elastic search(版本2.0.4.version)。我尝试使用不同bean名称在配置中创建2个elasticsearchoperation实例。我正在尝试使用带有索引(IndexQuery IndexQuery)方法的对象进行插入。但当我尝试插入时,我无法保持字段的映射(非分析字段类型)。可以有人请帮助我如

  • 我正在尝试配置Spring数据引导和ES项目在我的pom.xml我有: } 在我的pom xml中,我有这个dep: 这应该提供驱动程序,但我不断得到:描述: 无法确定数据库类型 NONE 的嵌入式数据库驱动程序类 行动: 如果你想要一个嵌入式数据库,请在类路径上放置一个受支持的数据库。如果要从特定配置文件加载数据库设置,则可能需要激活它(当前没有配置文件处于活动状态)。

  • 我试图用Spring Boot和弹性搜索设置一个应用程序。这个应用程序已经使用Spring Data JPA存储库来持久化我的实体。当我试图在启用弹性搜索配置的情况下运行应用程序时,我遇到的问题是,当存储库被扫描时,我得到了一个异常。 我得到了以下例外: 我的存储库的定义如下: 异常似乎是由于count查询的签名导致的,该签名返回一个int。尽管这个存储库可以很好地处理JPA,但它会抛出一个异常,