注意:-编辑了一些错字
我正在尝试将我的Spring Boot应用程序与Mongo Atlas连接起来。我已经配置了Atlas,我也能够通过Mongo指南针连接到Atlas,并且我已经通过画布创建了一个带有集合的数据库。然后我从atlas的connect选项卡中选择了java作为驱动程序复制了uri,尝试将所有版本一一指定,但没有任何效果。我的代码如下。
我的控制器
@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";
}
这是我的Entity类
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
好心的帮助,我已经苦苦挣扎了三天,但仍无法解决
答案 0 :(得分:0)
在经过大量搜索和试验后,如果它能对某人有所帮助,我终于解决了,我要做的就是在我的pom.xml中添加此依赖项。
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
当我已经添加spring-boot-starter-data-mongodb时,我不知道为什么需要添加此依赖项。无论如何,但这解决了我的问题