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);
}