How to Build Your First RESTful API with Spring Boot Series: Understanding the Springboot Architecture & Project Structure
Starting out on an entirely new framework can be very tedious, especially when you are so much used to the structure and architecture of different frameworks. This reminds me of my personal experience when I was starting out on Spring framework having worked so much with Django rest framework for about 2 years. It took me sometime before I could understand the entire framework, its structure, how the work flow is and how to connect the dots in terms of the structure. What the “How to Build Your First RESTful API with Spring Boot Series” seeks to achieve is to help a beginner in spring framework understand the architecture of the framework, device a proper project structure, understand how each component of the structure communicates and works and finally use this knowledge gained to build their first RESTFUL API in Springboot. 1. Architecture of the Spring Framework Spring framework uses the Model, View, Controller (MVC) architecture. Understanding this architecture will ensure proper separation of concerns and a well organised Springboot project which will ensure easier maintenance. Follow closely as I walk you through the components of this architecture; a. Model The model represents the aspects of the project that are associated with data representation, structure of data storage, data retrieval and logic performed on data. This typically includes the entity class, services, the Data Transfer Object (DTO) and Data Access Object (DAO) layers. Data Transfer Object (DTO) - For decoupling the database from external representation of data Entity Class – Defines the structure of data representation in the database. Service Layer – This is where the core business logic resides. Data Access Object – This is where core functions that performs operations on the database resides. This layer often abstracts the typical database queries and work together with ORM to perform its operations. In Springboot, this layer is the repository. b. View This layer contains components responsible of rendering the UI for the user. In modern applications, the UI is hosted separately from the backend. However, spring makes room for implementation where a developer will want to develop a full-stack application within a Spring project. This layer often includes html, jsp, thymleaf templates, and other view technologies. c. Controller The controller layer facilitates communication between the model and the view. It handles user requests and processes user inputs. 2. Flow within Spring’s MVC Architecture A user sends a request to the server (this is through the view layer). The request is intercepted by the DispatcherServlet. The dispatcher servlet in a layman’s understanding is comparable to a dispatch rider who picks up a package, determines the owner of package and delivers it to the rightful owner. This is what the dispatcherServlet does. Similarly, the dispatcherServlet intercepts the request and delegates the request to the suitable controller. The controller delegates the request to a thread which sends the request to the respective service class (in the Model layer) and may access the database using the Data Access Object (repository). The service class performs the necessary conversion to a data transfer object in case a response data is expected. The thread delegated by the controller returns the response and the dispatcherServlet selects an appropriate View using the ViewResolver. The response is rendered back to the user. 3. Standard Project Structure for Spring Projects This is the standardized folder that adheres to the MVC architecture and ensures clean separation of concerns. In the subsequent episodes of this series, we will pick a sample project, begin from each component within the project structure and start building each component. Don’t hesitate to reach out on my email.
Starting out on an entirely new framework can be very tedious, especially when you are so much used to the structure and architecture of different frameworks.
This reminds me of my personal experience when I was starting out on Spring framework having worked so much with Django rest framework for about 2 years. It took me sometime before I could understand the entire framework, its structure, how the work flow is and how to connect the dots in terms of the structure.
What the “How to Build Your First RESTful API with Spring Boot Series” seeks to achieve is to help a beginner in spring framework understand the architecture of the framework, device a proper project structure, understand how each component of the structure communicates and works and finally use this knowledge gained to build their first RESTFUL API in Springboot.
1. Architecture of the Spring Framework
Spring framework uses the Model, View, Controller (MVC) architecture. Understanding this architecture will ensure proper separation of concerns and a well organised Springboot project which will ensure easier maintenance.
Follow closely as I walk you through the components of this architecture;
a. Model
The model represents the aspects of the project that are associated with data representation, structure of data storage, data retrieval and logic performed on data. This typically includes the entity class, services, the Data Transfer Object (DTO) and Data Access Object (DAO) layers.
- Data Transfer Object (DTO) - For decoupling the database from external representation of data
- Entity Class – Defines the structure of data representation in the database.
- Service Layer – This is where the core business logic resides.
- Data Access Object – This is where core functions that performs operations on the database resides. This layer often abstracts the typical database queries and work together with ORM to perform its operations. In Springboot, this layer is the repository.
b. View
This layer contains components responsible of rendering the UI for the user. In modern applications, the UI is hosted separately from the backend. However, spring makes room for implementation where a developer will want to develop a full-stack application within a Spring project. This layer often includes html, jsp, thymleaf templates, and other view technologies.
c. Controller
The controller layer facilitates communication between the model and the view. It handles user requests and processes user inputs.
2. Flow within Spring’s MVC Architecture
- A user sends a request to the server (this is through the view layer).
- The request is intercepted by the DispatcherServlet.
- The dispatcher servlet in a layman’s understanding is comparable to a dispatch rider who picks up a package, determines the owner of package and delivers it to the rightful owner. This is what the dispatcherServlet does. Similarly, the dispatcherServlet intercepts the request and delegates the request to the suitable controller.
- The controller delegates the request to a thread which sends the request to the respective service class (in the Model layer) and may access the database using the Data Access Object (repository).
- The service class performs the necessary conversion to a data transfer object in case a response data is expected.
- The thread delegated by the controller returns the response and the dispatcherServlet selects an appropriate View using the ViewResolver.
- The response is rendered back to the user.
3. Standard Project Structure for Spring Projects
This is the standardized folder that adheres to the MVC architecture and ensures clean separation of concerns.
In the subsequent episodes of this series, we will pick a sample project, begin from each component within the project structure and start building each component.
Don’t hesitate to reach out on my email.