java - 带有 mongo atlas 的 Spring Boot 不起作用

标签 java spring mongodb spring-boot mongodb-atlas

注意:- 编辑了一些拼写错误

我正在尝试将我的 Spring Boot 应用程序与 Mongo atlas 连接。我已经配置了 Atlas,我也可以通过 Mongo compass 连接到 atlas,并且我已经通过 canvas 创建了一个包含集合的数据库。然后我从atlas的连接选项卡复制了uri,选择java作为驱动程序,尝试一一给出的所有版本,但没有任何效果。我的代码如下。

我的 Controller

    @GetMapping("/candidate")
public String getCandidate(Model model){
    model.addAttribute("cdto", new CandidateDto());
    return "candidate";
}

@PostMapping("candidate")
public String postCandidate(@ModelAttribute ("cdto") @Valid CandidateDto cdto){

    cservice.addCandidate(cdto);
    return "redirect:/candidate";
}

这是我的实体类

    import lombok.Data;

    @Data
    @Document(collection="candidate")
    public class Candidate {
        private String name;
        private String age;
        private String branch;
        private String institute;
        private String pasout;
        private String mobile;
        private String email;
        private String tenth;
        private String wealth;
        private String degree;


    }

这是我的 dto 类

    import lombok.Data;
    @Data
    public class CandidateDto {

      private String name;
      private String age;
      private String branch;
      private String institute;
      private String pasout;
      private String mobile;
      private String email;
      private String tenth;
      private String wealth;
      private String degree;


  }

这是我的存储库

    public interface CandidateRepository extends MongoRepository<Candidate, String> {

    } 

这是我的服务实现

    @Override
public void addCandidate(CandidateDto cdto) {
    Candidate ca=new Candidate();

    ca.setName(cdto.getName());
    ca.setAge(cdto.getAge());
    ca.setBranch(cdto.getBranch());
    ca.setDegree(cdto.getDegree());
    ca.setEmail(cdto.getEmail());
    ca.setInstitute(cdto.getInstitute());
    ca.setMobile(cdto.getMobile());
    ca.setPasout(cdto.getPasout());
    ca.setTenth(cdto.getTenth());
    ca.setTwelth(cdto.getTwelth());
    crepo.save(ca);

}

这是我的 application.properties

    spring.data.mongodb.database=ndtcdb



    spring.data.mongodb.uri=mongodb://myadmin:mypassword@cluster0-shard-00-00-vfmqk.mongodb.net:27017,cluster0-shard-00-01-vfmqk.mongodb.net:27017,cluster0-shard-00-02-vfmqk.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority

编辑:-为我的应用程序类添加此代码

    @SpringBootApplication
    public class NdtcApplication {

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

   }

下面是我的 pom.xml

    <dependencies>
    <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.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
    <groupId>javax.servlet</groupId>
     <artifactId>jstl</artifactId>
   </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
       </dependency>
       </dependencies>

这是我收到的错误

    java.lang.NoClassDefFoundError: com/mongodb/MongoCredential
at java.lang.Class.getDeclaredConstructors0(Native Method) ~[na:1.8.0_161]
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) ~[na:1.8.0_161]
at java.lang.Class.getDeclaredConstructors(Unknown Source) ~[na:1.8.0_161]
at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.findConstructorBindingAnnotatedConstructor(ConfigurationPropertiesBindConstructorProvider.java:62) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBindConstructorProvider.getBindConstructor(ConfigurationPropertiesBindConstructorProvider.java:48) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBean$BindMethod.forType(ConfigurationPropertiesBean.java:311) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:63) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at com.ndtc.NdtcApplication.main(NdtcApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    Caused by: java.lang.ClassNotFoundException: com.mongodb.MongoCredential
    at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_161]
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_161]
    ... 24 common frames omitted

请帮忙,我已经挣扎了三天但无法解决它

最佳答案

如果它对某人有帮助,经过大量的搜索和试验,我终于解决了它,我所做的就是在我的 pom.xml 中添加这个依赖项

    <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>

我不知道为什么我已经添加了 spring-boot-starter-data-mongodb 还需要添加这个依赖。无论如何,但这解决了我的问题

关于java - 带有 mongo atlas 的 Spring Boot 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60944474/

相关文章:

java - pyspark无法启动

java - aspectj 中的同步切入点

Java接口(interface)实现

java - TcpConnectionFactoryFactoryBean : using both SslContextSupport and SocketFactorySupport

java - 如何独立于 Spring 上下文使用 @Autowired 和 @Value 注释?

java - 如何使用 JavaCpp 将函数调用从 C++ 映射到 Java?

spring - 直接从 Spring 上下文获取 .properties 文件中的值

javascript - 来自外国集合的项目特定字段 $lookup 结果

javascript - 如何使用 mongoDB 和 Node.js 从集合中获取所有文档

node.js - 将 Node/Mongo 部署到 Openshift