Java Spring Boot 2.0连接 MongoDB 4.0时候出错。
抛出来一堆异常信息,最后找到问题根源,解决办法:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homeController': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property getAll found for type Users!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:586) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at
是Repository定义错误,没有getAll(),实体没有Allsh属性,所以一直报错。
实体类的定义:
@Document(collection = "Users")
public class Users {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Repository改成规范的方法名就可以了
public interface UsersRepository extends MongoRepository<Users, Integer> {
public List<Users> getUserByName(String name);
public Users getUserByNameAndPassword(String name, String password);
public Users getUserById(int id);
}