Spring Boot Project Structure
Let’s check the structure of the spring boot project. You can check the blog regarding the Spring Boot framework from the following link.
Firstly, we have to create a basic project of Spring Boot by using a build tool. We can select Maven or Gradle as the build tool. We can initiate a spring boot project from the following URL.
We can select the build tool, spring boot version, name of the project, & description of the project as per the top image. And also, we can add all the dependencies needed for the project. Mainly, we need Spring Web, Spring Data JPA, & MYSQL Driver as the dependencies. After we initiate the project, we need to define the server port of the project. Default port is 8080. But we can change the port by using the following code of the application. Properties file.
server.port=8080
Then we need to define database properties. In this example, we are using MYSQL as the database. According to the database type, properties & drivers will be changed. The following properties need to be inserted into the application. Properties file.
spring.datasource.url=jdbc:mysql://localhost:3306/database_name
spring.datasource.username=database_username
spring.datasource.password=database_password
spring.datasource.driverClassName=com.mysql.jdbc.Driver
Now let’s consider the structure of the spring boot project. There are mainly five levels in the spring boot project.
01. Controller Layer
When we consider the level of the project, this is the outer layer of the spring boot project. Also, we can refer to it as the user interaction layer. It means REST/SOAP endpoint level. Let’s consider the REST endpoint as an example. Following is the example code for the controller layer.
/**
* The role controller class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
@RestController
@RequestMapping(path = "/role")
public class RoleController {
@Autowired
private RoleService roleService;
@PostMapping(path = "/save")
public ResponseEntity<?> save(@RequestBody RolePermissionDto rolePermissionDto, @RequestParam int partyId) {
return ResponseEntity.ok(roleService.save(rolePermissionDto, partyId));
}
}
This is a saving endpoint of user roles. @RestController & @RequestMapping are the required annotations for the controller class. We can define posting type under endpoint method as @PostMapping, @GetMapping, @PutMapping, etc. Following is the sample URL for the example code.
http://localhost:8080/role/save
02. Service Interface Layer
The service interface will define all the service methods. It’s the intermediary between the controller class and the service implementation class. Following is the example code of the service interface.
/**
* The role service interface.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
public interface RoleService {
APIResponse<Boolean> save(RolePermissionDto rolePermissionDto, int partyId);
}
03. Service Implementation Layer
The service implementation layer is used to do all the method implementation of the service layer. This layer will include all the business logic. Following is the example code for the service implementation layer.
/**
* The role service implementations class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
@Service
public class RoleServiceImpl implements RoleService {
@Override
public APIResponse<Boolean> save(RolePermissionDto rolePermissionDto, int partyId) {
try{
saveRolePermissions(rolePermissionDto, partyId);
return new APIResponse<>(true, "Role registered successfully", true) ;
}catch (Exception exception){
throw exception;
}
}
}
@Service annotation is required for this layer. This annotation defines that this is the service layer of the Spring Boot project.
04. Repository Layer
This is the most important layer of the project. This layer is associated with the JPA. Simply JPA is the ORM of the spring boot. It means all the database-related data retrieval, insertion, update, & deleting operations are performed through this layer. Following is the example code for this layer.
/**
* The role repository interface.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
@Repository
public interface RolePermissionRepository implements JpaRepository<RolePermission, Integer> {
RolePermission save(RolePermissionDto rolePermissionDto);
}
This layer is an interface, and we have to implement the JPA Repository base interface. Now we can implement JPA methods, JPQL, or native queries to perform database operations. Following example associated with the JPA method. @Repository annotation is required for this layer.
05. Model Layer
This layer will contain the database table model mapping class. It means this class contains all the attributes the same as the database table. This is the database table model of the repository. This model has a one-to-one relationship with the repository level. Following is the example code of the model layer.
/**
* The role permission entity class.
*
* @author Sachith Ariyathilaka
* @version 1.0.0
* @since 2024/12/15
**/
@Entity
@Table(name = "ROLE_PERMISSION")
@Getter
@Setter
@NoArgsConstructor
public class RolePermission {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", updatable = false, nullable = false)
private int id;
@Column(name = "PERMISSION_ID")
private int permissionId;
@Column(name = "ROLE_ID")
private int roleId;
@Column(name = "ACTIVE")
private Boolean active;
@Column(name = "CREATED_DATE")
private LocalDate createdDate;
@Column(name = "LAST_MODIFIED_DATE")
private LocalDate lastModifiedDate;
@Column(name = "CREATED_USER")
private int createdUser;
@Column(name = "LAST_MODIFIED_USER")
private int lastModifiedUser;
}
We will use @Getter, @Setter, and @NoArgsConstructor to create the getters, setters, & null constructors. We can use the manual method to create these functions. But @Entity and @Table are required annotations for the level.