我使用的是Spring Boot 2.7.15,JDK 17

在pom.xml中添加了用于数据库通信的Spring data JPA、mongo-db依赖项

错误日志(log)我看到了一些解决方案,但我没有应用程序.yml文件来更改‘url’


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.7.15)

2023-08-25 09:00:35.880  INFO 10812 --- [           main] c.lcwd.rating.RatingServiceApplication   : Starting RatingServiceApplication using Java 17.0.8 on pushpendra-ThinkPad-T460 with PID 10812 (/home/pushpendra/Documents/Workspace/Intellij/Microservice/RatingService/target/classes started by pushpendra in /home/pushpendra/Documents/Workspace/Intellij/Microservice/UserService)
2023-08-25 09:00:35.886  INFO 10812 --- [           main] c.lcwd.rating.RatingServiceApplication   : No active profile set, falling back to 1 default profile: "default"
2023-08-25 09:00:37.529  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-08-25 09:00:37.532  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-08-25 09:00:37.559  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11 ms. Found 0 JPA repository interfaces.
2023-08-25 09:00:37.644  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2023-08-25 09:00:37.644  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2023-08-25 09:00:37.646  INFO 10812 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 MongoDB repository interfaces.
2023-08-25 09:00:38.671  INFO 10812 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8083 (http)
2023-08-25 09:00:38.687  INFO 10812 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-08-25 09:00:38.688  INFO 10812 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.79]
2023-08-25 09:00:38.841  INFO 10812 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-08-25 09:00:38.842  INFO 10812 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2835 ms
2023-08-25 09:00:38.964  WARN 10812 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2023-08-25 09:00:38.974  INFO 10812 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-08-25 09:00:38.997  INFO 10812 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-08-25 09:00:39.020 ERROR 10812 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


Process finished with exit code 1
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.7.15</version>
       <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lcwd.rating</groupId>
    <artifactId>RatingService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RatingService</name>
    <description>This is Rating Service</description>
    <properties>
       <java.version>17</java.version>
    </properties>
    <dependencies>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-mongodb</artifactId>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

       <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
       </dependency>
       <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
       </dependency>
    </dependencies>

    <build>
       <plugins>
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
             <configuration>
                <excludes>
                   <exclude>
                      <groupId>org.projectlombok</groupId>
                      <artifactId>lombok</artifactId>
                   </exclude>
                </excludes>
             </configuration>
          </plugin>
       </plugins>
    </build>

</project>

这是我的申请表.yml

spring:
  data:
    mongodb:
      uri: mongodb://userAdmin:admin@localhost:27017/?authMechanism=DEFAULT
      database: microservices

server:
  port: 8083
package com.lcwd.rating;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RatingServiceApplication {

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

}

到目前为止,只有数据库配置添加到项目中,但当我运行项目时,我得到了标题中提到的错误.

还可以try 使用uri,如下所示

mongodb://userAdmin:admin@localhost:27017

我还可以try 使用Application.Properties而不是Applation.yml

仔细判断用户名、密码、主机、端口号和数据库是否也存在.

重新启动IntelliJ,然后再次运行.

查看Stackoverflow问题,在Google上搜索,参考文章,try 与chat GPT也.

推荐答案

Explanation:在使用mongdb数据库时,您不需要spring-boot-starter-data-jpa依赖项,因为JPA(Java持久性API)是用于管理关系数据库的规范,而MongoDB是一个NoSQL数据库.与传统的关系数据库相比,MongoDB使用了不同的数据模型和查询语言.

Solution:只需从pom.xml中删除以下依赖项,它就可以很好地工作.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Mongodb相关问答推荐

如何拉平mongo DB中的对象并在根目录中保存时有条件地重命名字段?

MongoDB索引使用

会话的Mongo-go驱动程序版本.Copy()

在出现错误时忽略mongodb事务回滚是好做法吗

Golang mongodb聚合错误:管道阶段规范对象必须包含一个字段

使用golang的MongoDB错误无法访问服务器

Mongoose $inc 枚举验证失败

如何将 json 字符串编组到 bson 文档以写入 MongoDB?

Mongoose 架构引用和未定义类型ObjectID

哪个库最适合用于带有 Scala 的 MongoDB?

如何在 MongoDB 集合中查找与给定条件匹配的文档和单个子文档

在 URL 中使用 ID(来自 mongo 的 ObjectId)是否安全?

适用于 Windows 10 64 位的 MongoDB 下载

MongoDB Java API:put() 与 append()

遍历所有 Mongo 数据库

在 mongodb 中插入当前日期时间

MongoDb 聚合 $match 错误:Arguments must be aggregate pipeline operators

Mongodb同时在多个字段上聚合(计数)

如何在 Windows 上停止 mongodb 服务器?

findOneAndUpdate 中的文档未更新