【问题标题】:Connect to MongoDb using Spring Boot使用 Spring Boot 连接到 MongoDb
【发布时间】:2021-03-31 17:10:41
【问题描述】:

我想通过 aplication.properties 文件将 Spring Boot REST Api 项目连接到 MongoDb。为什么?因为这对我来说似乎更容易。

我知道如何与 MySQL 数据库建立这种连接。我已经下载了 MongoDb Compass GUI。

application.properties 文件

spring.data.mongodb.uri=mongodb://localhost:27017/springtest
spring.data.mongodb.username=mihai
spring.data.mongodb.password=mihai

我使用 uri 因为我发现如果 MongoDb 版本 > 3.x.x 你应该使用它。我的 MongoDb 版本是 4.4.4

用户收藏:link

pom 文件:

    <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-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.12.8</version>
        </dependency>


        <!--##################################################-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.4.4</version>
            </plugin>
        </plugins>
    </build>

</project>

用户存储库文件:

public interface UserRepository extends MongoRepository<Users, String> {

}

主应用程序文件:

@SpringBootApplication(exclude={SecurityAutoConfiguration.class})
public class AdServicesApplication {

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

}

我得到以下错误恍惚:

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 0

根据我的研究,我发现问题可能是由于 application.properties 文件中提供的配置,但我真的不知道如何为 MongoDb 正确编写它。

例如,如果我将 application.properties 内容更改为:

spring.datasource.url=jdbc:mysql://localhost:3306/employee_directory?useSSL=false
spring.datasource.username=mihai
spring.datasource.password=mihai

效果很好。

谢谢!

【问题讨论】:

    标签: spring mongodb spring-boot


    【解决方案1】:

    尝试删除依赖

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

    并使用配置

    spring.data.mongodb.uri=mongodb://localhost:27017/springtest
    spring.data.mongodb.username=mihai
    spring.data.mongodb.password=mihai
    

    当您添加依赖项时,它需要一个您未提供的数据源 url

    【讨论】:

    • 我做到了。现在我有以下错误:Consider defining a bean of type com.servizone.app.dao.UserRepository。我的所有包都在主包应用程序下方。我正在努力调试它,但我找不到方法,因为所有解决方案都是关于使用 ComponentScan 如果您有正确的包层次结构并且还使用我拥有的 @Repository 注释,则不需要已经试过了。
    • 我用相同的文件再次启动了项目,服务器开始购买我在访问 localhost:8080 时收到白标签错误页面:This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Apr 01 03:48:31 CEST 2021 There was an unexpected error (type=Not Found, status=404). No message available
    • 如果没有映射是正常的。显示您的控制器并提出另一个问题,您的问题已解决
    • 我没有控制器。据我了解,如果我扩展 MongoRepository,我将免费获得 Users 类的 crud 方法和 endpoints。如果我错了,请纠正我。就像在 JPA 存储库中一样 link
    • 我通过添加依赖项使其工作:&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt; 以下是所有其他项目依赖项:&lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt; &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    猜你喜欢
    • 2018-06-10
    • 2016-05-12
    • 2015-07-14
    • 2017-09-04
    • 2017-10-26
    • 2017-07-10
    • 2016-09-13
    • 2021-03-15
    • 2017-11-14
    相关资源
    最近更新 更多