我正在尝试使用MongoDB在春季启动中创建数据库,并且不断出现两个错误。
我遇到的第一个错误是内置的MongoDB函数findAll
,它给了我一个错误Failed to instantiate com.accountservice.accountservice.Models.User using constructor NO_CONSTRUCTOR with arguments
,我不太明白为什么会得到这个错误。
我遇到的第二个问题是,当我尝试在数据库中查找特定用户并使用findbyFirstName
时,这实际上并没有给我带来错误,但什么也不返回。
这是我的代码;
用户类别;
@Document(collection = "AllUsers")
public class User {
private String bloodGroup;
private String firstName;
@Indexed(direction = IndexDirection.ASCENDING)
private String _surname;
private String _email;
private String _password;
private String _addressline;
private String _postcode;
// Constructor // Getters Setters
帐户服务类别;
@Service
public class AccountService {
@Autowired
private UserRepository UserRepo;
public User getByfirstName(String firstName){
return UserRepo.findByFirstName(firstName);
}
public List<User> getAll(){ // This does not work
return UserRepo.findAll();
}
}
用户存储库类;
@Service
@Repository
public interface UserRepository extends MongoRepository<User, String> {
public User findByFirstName(String firstName);
}
控制器类;
@Component
public class As_Controller {
@Autowired
private AccountService Service_functions;
private UserRepository UserRepo;
public As_Controller(UserRepository userRepo) {
UserRepo = userRepo;
}
@ResponseBody
@GetMapping("/getUser/{firstName}") // This does not return anything
public User getUser( @PathVariable String firstName ){
System.out.println("Working"); // This prints in console
return Service_functions.getByfirstName(firstName);
}
@GetMapping("/getAll")
public List<User> getAllUsers(){ // This does not work
return Service_functions.getAll();
}
}
在此问题上的任何帮助/建议将不胜感激!