尝试使用Spring启动应用程序配置MongoDB时出现以下错误:
org.springframework.beans.factory.BeanDefinitionStoreException:无法处理配置类的导入候选[org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration];嵌套异常是java.lang.NoClassDefFoundError:org / springframework / data / repository / config / RepositoryConfigurationSource
Code is as below
package com.women.code.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAutoConfiguration
public class WomenInCodeApplication {
public static void main(String[] args){
SpringApplication.run(WomenInCodeApplication.class, args);
}
}
@RestController
@RequestMapping("/USER/WomenInComputing/")
public class WomenInCodeMgmtController {
@Autowired
private QueryDb queryDb;
@RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public UserRequest add(@RequestBody UserRequest userRequest) {
return queryDb.saveToRepository();
}
}
@Component
public class QueryDb implements UserRepository {
@Autowired
private UserRepository repository;
public UserRequest saveToRepository(){
UserRequest userRequest = new UserRequest();
userRequest.setFirstName("Isaac");
userRequest.setLastName("Lavoie");
return repository.save(userRequest);
}
@Override
public List<UserRequest> save(Iterable<? extends UserRequest> entites) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<UserRequest> findAll() {
// TODO Auto-generated method stub
return null;
}
@Override
public List<UserRequest> findAll(Sort sort) {
// TODO Auto-generated method stub
return null;
}
@Override
public Page<UserRequest> findAll(Pageable pageable) {
// TODO Auto-generated method stub
return null;
}
@Override
public UserRequest save(UserRequest entity) {
// TODO Auto-generated method stub
return null;
}
@Override
public UserRequest findOne(String id) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean exists(String id) {
// TODO Auto-generated method stub
return false;
}
@Override
public Long count() {
// TODO Auto-generated method stub
return null;
}
@Override
public void delete(UserRequest entity) {
// TODO Auto-generated method stub
}
@Override
public void delete(Iterable<? extends UserRequest> entities) {
// TODO Auto-generated method stub
}
@Override
public void deleteAll() {
// TODO Auto-generated method stub
}
@Override
public UserRequest findByFirstName(String firstName) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<UserRequest> findByLastName(String lastName) {
// TODO Auto-generated method stub
return null;
}
}
public interface UserRepository extends MongoRepository<UserRequest, String> {
public UserRequest findByFirstName(String firstName);
public List<UserRequest> findByLastName(String lastName);
public UserRequest saveToRepository();
}
public class UserRequest {
@Id
private String id;
private String firstName;
private String lastName;
private String username;
private String password;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserRequest [firstName=" + firstName + ", lastName=" + lastName + ", username=" + username
+ ", password=" + password + "]";
}
}
的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WomenForCode</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- <start-class>com.women.code.controller.WomenInCodeMgmtController</start-class> -->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>