首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring data elasticsearch 4.1

Spring data elasticsearch 4.1
EN

Stack Overflow用户
提问于 2021-10-26 16:11:28
回答 3查看 233关注 0票数 0

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

代码语言:javascript
复制
public class Entity implements Serializable {
    @Id private String id;
    private URL url;
    ...
}

这对于Jackson的spring data 3.0来说工作得很好,但是在升级到4.0之后,Jackson不再可用,现在我得到了一个来自spring的实例化异常,无法实例化URL对象。

例外:

代码语言:javascript
复制
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

任何关于解决方案的想法都很感谢。

EN

回答 3

Stack Overflow用户

发布于 2021-10-26 16:27:43

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

在您的例子中,构造函数应该是这样的。

代码语言:javascript
复制
public Entity(URL url, ** **) {
    this.url = url;
    **;
    **;
}

此外,你还可以使用Lombok通过@NoArgsConstructor,@AllArgsConstructor,@Data这样的注解来获得构造函数的功能。只需将其添加到你的模型类中,它就会工作。

票数 0
EN

Stack Overflow用户

发布于 2021-10-27 05:45:40

从索引中的数据可以看出,URL被存储为嵌套对象url,其属性为protocolhostportfileauthoritypathhandler (本身就是一个对象)。

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

要读入该内容,您需要定义一个自定义转换器,该转换器可以读写Document对象(基本上是一个Map<String, Object>并注册该转换器(参见https://docs.spring.io/spring-data/elasticsearch/docs/4.2.5/reference/html/#elasticsearch.mapping.meta-model.conversions) )

票数 0
EN

Stack Overflow用户

发布于 2021-11-02 20:48:21

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

代码语言:javascript
复制
    @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();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69726566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档