使用Spring Boot创建mangodb应用程序时出现问题

时间:2019-07-15 04:16:52

标签: mongodb spring-boot

我正在设置一个春季启动应用程序,以显示学年中学生的成绩,但遇到了一些奇怪的错误……我在各处都找到了解决方案,但是在任何地方都找不到任何解决方案,所以我来问为此,这是我的问题。

控制台中有错误:https://pastebin.com/DEtinGYA

这是application.properties文件:

spring.data.mongodb.uri=mongodb+srv://iabdou:<11223344>@maurimesbeta-tayti.mongodb.net/test?retryWrites=true&w=majority
spring.data.mongodb.database=test

主应用程序类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MaurimesBackEndApplication implements CommandLineRunner{
    @Autowired
    private StudentRepository repository;   
    public static void main(String[] args) {
        SpringApplication.run(MaurimesBackEndApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        System.out.println(repository.findAll());
    }
}

MyBean类

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;

@Data
@Document(collection = "students")
public class Student {
    @Id
    private String id;
    private int matricule;
    private String name;
    private String school;
    private String region;
    private boolean succeed;
    private Double moyenne; 
    public Student(int matricule, String name, String school, String region,
            boolean succeed, Double moyenne) {
        this.matricule = matricule;
        this.name = name;
        this.school = school;
        this.region = region;
        this.succeed = succeed;
        this.moyenne = moyenne;
    }
}

MyBean的存储库类

import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

public interface StudentRepository extends MongoRepository<Student, String> {
    Student findFirstByName(String name);   
    @Query("{matricule:?0}")
    List<Student> findCustomByMatricule(String matricule);  
}

1 个答案:

答案 0 :(得分:0)

从您的堆栈跟踪中可以看到:

org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    at org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration$$EnhancerBySpringCGLIB$$fb05db05.mongo(<generated>) ~[spring-boot-autoconfigure-2.1.6.RELEASE.jar:2.1.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_211]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_211]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_211]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
    ... 81 common frames omitted
Caused by: javax.naming.CommunicationException: DNS error

因此,在尝试连接到mongo实例时,您的spring-boot应用程序在尝试解析DNS时会出现连接超时。

Caused by: javax.naming.CommunicationException: DNS error

您可以验证您的计算机是否可以访问 您在 application.properties 中列出的mongo服务器。另外,尝试直接在application.properties中提供mongo实例的ip地址。