Home > other >  Am I still using spring-mvc (MVC architecture) if my backend only returns a json response to the fro
Am I still using spring-mvc (MVC architecture) if my backend only returns a json response to the fro

Time:01-12

There are a lot of definitions on what MVC is, but most of them say that the user contacts the controller and then the controller changes the view and sends it to the user. If I am using React.js for my frontend and calling endpoints in my controller, am I still using MVC pattern or not? Is the @Controller even the same thing as the controller in MVC because in the definitions it says that the controller handles the application logic which in my case it does not I have service classes for that. I am writing an essay and I don't even know what type of application I am creating. It is driving me nuts.

CodePudding user response:

Yep, it's still MVC. In this case you have controller (Spring @Controller or @RestController and React) model (services) and view (html generated by React). There is no strict definition what is MVC.

In MVC controller should not contain logic themself, but communicate with model (your services), this is the correct layered backend architecture. Otherwise, the Single Responsibility Principle is violated.

CodePudding user response:

Spring Controller i.e., @Controller is the responsible person to provide the view the user requested if you need only the view page, it can be anything .html,.JSP and many more. So, if the user requested for any page either by the react.js or anything like angular then it calls the dispatcher servlet of the spring front controller and calls the handler mapper to which controller the user have requested.

If you requested something from the client to provide or do some crud operation then Rest Controller comes into the picture where @RestController...@Service...@Repository is been used.

So indirectly, the spring provides the way where developers can define the user request in the model view and controller format in which it segerrate the things separately and provides more convenient way to organise the code it looks like and spring have assigned annotations where it marks the class the responsibility it should behave.

  • Related