我正在尝试制作一个弹簧引导应用程序,并将其与多模块项目中的mongodb连接。但是我无法正确设置弹簧靴。我在运行应用程序时遇到错误。项目结构:--MainModule------SubModule-----------Controller(UsersController.java)-----------Main(UserApplication.java)-----------Model(Users.java and Address.java)-----------Repository(UsersRepository.java)-----------Service(UsersService.java)用户应用程序.java@SpringBootApplication@ComponentScan({"repository","model","service"})@EnableMongoRepositories("repository.UsersRepository")public class UsersApplication { public static void main(String[] args) { SpringApplication.run(UsersApplication.class, args); }}用户控制器.java @RestController class UsersController { @Autowired private UsersService usersService; @RequestMapping(value = "/create" , method = RequestMethod.POST) public Users create(@RequestBody Users user) { return usersService.createUser(user); } @RequestMapping("/get/{id}") public Optional<Users> getUser(@PathVariable int id) { return usersService.getById(id); } @RequestMapping("/getAll") public List<Users> getAll(){ return usersService.getAll(); } @RequestMapping("/update/{id}") public Users update(@RequestBody Users user) { Users u = usersService.updateUser(user); return u; } @RequestMapping("/delete") public String delete(@PathVariable int id) { usersService.deleteUser(id); return "Deleted "+id; } @RequestMapping("/error") public String err(){return "error";}}用户存储库.java@Repositorypublic interface UsersRepository extends MongoRepository<Users, Integer> {}用户服务.java@Servicepublic class UsersService { @Autowired private UsersRepository usersRepository; //create a user public Users createUser(Users user) { return usersRepository.save(user); } //retrive all users public List<Users> getAll() { return usersRepository.findAll(); } //retrive a user public Optional<Users> getById(int id) { return usersRepository.findById(id); }
2 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
EnableMongoRepositories
作为参数。它列出了要扫描存储库的软件包,请尝试以下操作:basePackages
@EnableMongoRepositories("repository")
喵喵时光机
TA贡献1846条经验 获得超7个赞
--MainModule
------SubModule
------Main(UserApplication.java)
-----------Controller(UsersController.java)
-----------Model(Users.java and Address.java)
-----------Repository(UsersRepository.java)
-----------Service(UsersService.java)
添加回答
举报
0/150
提交
取消