【问题标题】:Spring Data ElasticSearch NullPointerExceptionSpring Data ElasticSearch NullPointerException
【发布时间】:2014-09-22 10:58:44
【问题描述】:

我刚刚开始使用 Spring Data ElasticSearch。我已经实现了自己的存储库,但是如果我尝试保存实体,则会收到空指针异常。我有以下代码,这只是一些测试代码。

package org.test.elasticsearch.models;

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

@Document(indexName = "test", type = "book", shards = 1, replicas = 0)

public class Book {

@Id
private String id;
private String title;
private String author;

public Book(final String id, final String title, final String author) {
    this.id = id;
    this.title = title;
    this.author = author;
}

public String getId() {
    return this.id;
}

public void setId(final String id) {
    this.id = id;
}

public String getTitle() {
    return this.title;
}

public void setTitle(final String title) {
    this.title = title;
}

public String getAuthor() {
    return this.author;
}

public void setAuthor(final String author) {
    this.author = author;
}

}

package org.test.elasticsearch.configs;

import org.elasticsearch.node.NodeBuilder;
import org.test.elasticsearch.repositories.implementations.BookRepositoryImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@Configuration
@EnableElasticsearchRepositories("org.test.elasticsearch.repositories")
public class ElasticsearchConfiguration {

@Bean
public ElasticsearchOperations elasticsearchTemplate() {
    final NodeBuilder nodeBuilder = new NodeBuilder();
    return new ElasticsearchTemplate(nodeBuilder.local(true).clusterName("elasticsearch").node().client());
}

@Bean
public BookRepositoryImpl bookRepositoryImplementation() {
    return new BookRepositoryImpl();
}

}

package org.test.elasticsearch.repositories;

import org.test.elasticsearch.models.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

public interface BookRepository extends ElasticsearchCrudRepository<Book, String>, BookRepositoryCustom {

    // query methods

}

package org.test.webapp;

import org.test.elasticsearch.models.Book;
import org.test.elasticsearch.models.Book.BookBuilder;
import org.test.elasticsearch.repositories.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Test {

@Autowired
static BookRepository BookRepository;

public static void main(final String[] args) {
    SpringApplication.run(test.class, args);

    final Book testBook = new Book("12345", "TestTitle", "TestAuthor");
    BookRepository.save(testBook);
}

}

这是我的代码。运行我的 Spring Boot 应用程序后,我收到以下消息。

Exception in thread "main" java.lang.NullPointerException
at org.test.webapp.test.main(Test.java:24)

有人有想法吗?还有一个问题:我什么时候应该在自定义存储库上使用 ElasticsearchTemplate 和 IndexQuery 来保存我的实体?

【问题讨论】:

    标签: spring elasticsearch spring-data spring-boot spring-data-elasticsearch


    【解决方案1】:

    实际上,您的问题在于 Spring Boot:您没有正确使用它。您的 Test 类应该如下所示(没有“静态”访问 BookRepository 并且以不同的方式进行):

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.test.elasticsearch.models.Book;
    import org.test.elasticsearch.repositories.BookRepository;
    
    @Configuration
    @ComponentScan(basePackages = {"org.test.elasticsearch.configs", "org.test.webapp"})
    @EnableAutoConfiguration
    public class Test implements CommandLineRunner {
    
        @Autowired
        private BookRepository bookRepository;
    
        @Override
        public void run(String... args) {
            final Book testBook = new Book("12345", "TestTitle", "TestAuthor");
            bookRepository.save(testBook);
        }
    
        public static void main(final String[] args) {
            SpringApplication.run(Test.class, args);
        }
    }
    

    因为,正如我所见,您不想使用 Web 应用程序,因此您只需要 pom.xml 中的这些依赖项。与spring-boot-web无关:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
                <version>1.0.0.RELEASE</version>
            </dependency>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    相关资源
    最近更新 更多