SpringBoot | ElasticSearch |不支持的ID类型

问题描述 投票:2回答:1

[我正在尝试创建一个应用程序,在其中我可以通过spring boot和java执行弹性搜索功能,但是我正面临java.lang.IllegalArgumentException:不支持的ID类型类elastic.model.Identifier

这是代码的外观

我有模型课

package elastic.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName="customCustomer", type="elkcustomer")
public class Customer {

    @Id
    private Identifier identifier;
    private Order order;
    private String homeAddress;
    private String contact;
    //getters, setters, hashcode, equals, toString()

标识符为

package elastic.model;

import java.io.Serializable;

public class Identifier implements Serializable {

    private String id;
    private String firstName;
    private String lastName;
    //getters, setters, hashcode,equals, toString()

订购为

package elastic.model;

import java.io.Serializable;

public class Order implements Serializable {

    private int orderId;
    private String orderDescription;
    private float price;
    //getters, setters, hashcode, equals, toString

存储库为

package elastic.repository;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import elastic.model.Customer;
import elastic.model.Identifier;

@Repository
public interface CustomerRepository extends ElasticsearchRepository<Customer, Identifier> {

}

和主要跑步者为

package elastic.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import elastic.model.Customer;
import elastic.model.Identifier;
import elastic.model.Order;
import elastic.repository.CustomerRepository;

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages="elastic")
public class ElasticSearchApplication implements CommandLineRunner {

    @Autowired private CustomerRepository customerRepository;

    public static void main(String[] args) {
        SpringApplication.run(ElasticSearchApplication.class);
    }

    public void run(String... args) throws Exception {
        this.customerRepository.deleteAll();
        saveCustomers();
    }

    private void saveCustomers() {
        this.customerRepository.saveAll(generateCustomers());
    }

    private List<Customer> generateCustomers() {
        List<Customer> customers = new ArrayList<Customer>();

        Customer customer1 = new Customer();
        Identifier identifier1 = new Identifier();
        identifier1.setId("1001");
        identifier1.setFirstName("Rahul");
        identifier1.setLastName("Lao");
        customer1.setIdentifier(identifier1);
        Order order1 = new Order();
        order1.setOrderId(1);
        order1.setOrderDescription("Order 1");
        order1.setPrice(23.45f);
        customer1.setOrder(order1);
        customer1.setHomeAddress("Germany");
        customer1.setContact("9874563210");

        customers.add(customer1);

        return customers;
    }

}

我面对的例外是

ERROR SpringApplication Application startup failed
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticSearchApplication': Unsatisfied dependency expressed through field 'customerRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:581)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:367)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1340)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:751)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:387)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233)
    at elastic.service.ElasticSearchApplication.main(ElasticSearchApplication.java:24)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1060)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:578)
    ... 19 more
Caused by: java.lang.IllegalArgumentException: Unsupported ID type class elastic.model.Identifier
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory.getRepositoryBaseClass(ElasticsearchRepositoryFactory.java:90)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.lambda$getRepositoryInformation$2(RepositoryFactorySupport.java:378)
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryInformation(RepositoryFactorySupport.java:376)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:294)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$3(RepositoryFactoryBeanSupport.java:287)
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:141)
    at org.springframework.data.util.Lazy.get(Lazy.java:63)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:290)
    at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactoryBean.afterPropertiesSet(ElasticsearchRepositoryFactoryBean.java:67)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1763)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1700)
    ... 29 more

我该如何解决这个问题,以便我可以将自定义类作为ID,而不仅仅是像String,long,int这样的原始数据类型作为id。

谢谢。

java elasticsearch spring-boot spring-data-elasticsearch
1个回答
0
投票
public interface CustomerRepository extends ElasticsearchRepository<Customer, Identifier> { }
ElasticsearchRepository无法识别Identifier,请尝试String
© www.soinside.com 2019 - 2024. All rights reserved.