'Does a simple REST project in Spring Boot use the MVC principle?

I need to create a simple project that uses the Model-View-Controller principle and a MySQL database. And I want to use Spring Boot with Spring MVC and Spring Data JPA.

And I want to make GET, POST, PUT and DELETE requests that call the database and send a JSON to the client.

@GetMapping(value = "/users")
public Users getUsers() {
   // call the service -> call the database
}

And the response will be:

{
  "name": "John",
  "age": 45,
  ...
}

Does this project use the MVC principle? Or do I need to use a .jsp for the view to have a complete MVC principle?

So the Controller is the REST Controller and the Model is the Users POJO. And if this project use the MVC principle can somebody explain where is the view?

And if the service calls the repository and fetch the data from the MySQL database I want to know if the MVC is modified by adding the DAO, or the DAO is a part of the Model?



Solution 1:[1]

MVC is an architectural design pattern for the layers of your application. It is exactly about inner work of your application.

REST is how does your application interact with other applications.

You can combine them together at one application.

In general they are different patterns for different problems:

MVC you are receiving request -> process it (fetching the data from DB or with some ways) -> render it to the view -> and view with requested data return to the caller.

REST (Representational State Transfer) flow is quite similar. However, instead of return view with the data -> your app sends just representation of the data. The content type of response should be specified by the caller at request.

Now go one by other parts of your questions. (Maybe, it is even too many questions just for one question to answer them properly)

Does this project use the MVC principle? Or do I need to use a .jsp file for the view to have a complete MVC principle?

From the snipped which you have already shared - you used REST - and your method return JSON representation of resource.

For now looks like it is the desired goal which you want to achieve:

And I want to make GET, POST, PUT and DELETE requests that call the database and send a JSON to the client.

If you want to use MVC you have to return a rendered view with fetched data instead. Keep in the mind that you have to return HTML page to the caller at that case:

@Controller 
@RequiredArgsConstructor
public class ControllerDemo {
    private final UserService userService;

    @GetMapping(value = "/users")
    public String getAllUsers(Model model) { 
       // add fetched data to model as attribute
       model.addAtribute("users", userService.findAll());
       // view resolver should be configured to render response to `response-page.ftl` for example
       return "response-page"; 
    }

It doesn't matter if you use JSP or Freemarker or Mustache, etc - the main idea is that you create template and data from DB will be represented according to this template on HTML page.


So the Controller is the REST Controller and the Model is the Users pojo. And if this project use the MVC principle can somebody explain where is the view? - with REST approach is no view, the response at this case is representation of resource.


And if the service calls the repository and fetch the data from the MySQL database I want to know if the MVC is modified by adding the DAO, or the DAO is a part of the Model?

DAO stands for Data Access Layer. You a have a model - User class which describes your entity. And you store some user entities at DB. And you want to receive the all users which have already been stored (in real life you need to have pagination but we omit it for simplicity).

Thus for doing it you have to go row by row for your users table and convert the row from DB to the User entity. And you take this retrieving to separate data layer - DAO.
DAO is not part of the Model it is separate layer between Model and DB.

You could not do it at your service layer (UserService) because it will brake the Simple Responsibility Principle (SOLID Principles) for the service layer - do one job and do it well.

At the same time you support high cohesion for parts of your app (GRASP Principles)

Solution 2:[2]

MVC is the pattern to implement UI. So if you are just developing a REST API, no UI is involved and hence you do not need to concerned whether it is implemented as MVC or not.

Instead if you are developing a web application , the view is the technology to render HTML codes to the browser . Usually they are just a template engine such as JSP , FreeMarker or Thymeleaf which allows you to use their expression language to access the data in some Java object (i.e. Model) to define the HTML code that you want to generate as a template.

In Spring MVC , you need to use @RestController for developing a REST API while @Controller for the web application. The main difference is that @RestController includes @ResponseBody which will by-pass the view resolution process as there are no view in the REST API.

In short, as you are developing a REST API now , there are no UI and no view. Hence MVC is not applicable in here and you do not need to worry whether it is implemented based on MVC or not.

And if the service calls the repository and fetch the data from the MySQL database I want to know if the MVC is modified by adding the DAO, or the DAO is a part of the Model?

DAO is an object that is responsible for getting and managing the data from DB. It is nothing to do with Model.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2