Skip to content
Advertisement

correct structure in MVC with spring

I’m a little confused about correct mvc pattern.

This is my config file: In this class I’ve got all Beans.

JavaScript

This is my interface UserRepo, and interface UserService. They are the same

JavaScript

I’ve got my class that implemets this interface

JavaScript

And finally I’ve got my Controller In my Controller I @Autowired UsersRepo/it’s a inteface/. And my code works, I can do all CRUD operations.

But, was told to me that this is not the right way. I can’t autowire directly. @Autowired of UserRepo, inside Controller class. So I search info on web, and I create a Service class. The Service it’s made so: have the same interface with the same methods that I wrote inside UserRepo Inteface. After I create the class that implements that interface, called UserSeviceImpl.

In userServiceImpl I go to @Autowire UserRepo interface, and after I go to @Autowire userService inside the Controller.

But now my code doesn’t work, Ive got 404 status in all pages in all Controlles: `The requested resource [/bookProject/] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.`

I’ve got no errors inside Console, just info:

JavaScript

So I can not understand if I’m wrong to Autowire some bean, or if my current pattern is wrong, or if I need to change something in the config class, or if I have not applied the class of service well. Beacuse before my code was working well.

This is my controller: @Controller public class UserController { @Autowired private UserService us; List user; @GetMapping(“/new”) public ModelAndView new(Model model) {

JavaScript

And this is UserServiceImpl:

JavaScript

Advertisement

Answer

I would say check your path that you are calling against the controller definition. I think it help if you update the code to see also the controller.

Example:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement