【问题标题】:Can not use ElasticsearchRepository in spring boot无法在 Spring Boot 中使用 ElasticsearchRepository
【发布时间】:2021-12-20 11:43:37
【问题描述】:

我正在尝试使用 ElasticsearchRepository 进行简单的 CRUD 操作。我不想使用RestHighLevelClient。我在关注this tutorials 第一种方法。 这是我的依赖

    implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'

Application.properties 中的属性

spring.elasticsearch.rest.uris=http://192.168.5.212:9200
spring.elasticsearch.rest.connection-timeout=10s
spring.elasticsearch.rest.read-timeout=1m
spring.elasticsearch.rest.username=user
spring.elasticsearch.rest.password=pass

然后我刚刚添加了存储库

@Repository
public interface UserRepo extends ElasticsearchRepository<User, String> {

}

最后是我的用户类

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Document(createIndex = false, indexName = "user", type = "_doc")

public class User implements Serializable {

    @Id
    private String uid;

    private String name;
}

但我得到了错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-07 10:16:30,497 ERROR [main] o.s.b.d.LoggingFailureAnalysisReporter:40 - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in com.user.UserService required a bean named 'elasticsearchTemplate' that could not be found.


Action:

Consider defining a bean named 'elasticsearchTemplate' in your configuration.


尝试了#spring.data.elasticsearch.repositories.enabled = true ,但仍然出现错误

注意这个问题不是 This question 的重复,因为我使用了不同的方法。

【问题讨论】:

  • 请附上完整的错误信息。
  • @tgdavies 没有得到任何异常只是这个错误消息。请检查更新的问题

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


【解决方案1】:

解决了我的问题,问题是我的springboot项目版本是'2.1.8.RELEASE'。 我尝试实现的自动配置需要最低 Spring Boot 版本2.2.0。 由于我无法升级spring boot版本,我不得不手动添加配置类。

【讨论】:

    猜你喜欢
    • 2016-08-05
    • 1970-01-01
    • 2020-09-22
    • 2018-07-17
    • 2018-07-04
    • 2019-07-29
    • 2022-01-25
    • 2020-08-08
    • 2020-08-25
    相关资源
    最近更新 更多