Spring Boot - Project Structure Practices

Ömer Kurular
4 min readMay 11, 2021

In this article, I am going to talk about package structures for Spring Boot applications and give the one I create whenever I started a new project.

Proper packaging in a project is important because when the project grows or someone else is assigned to your project, it must be easy to get around different kind of files. For example, when you need a file of type model, you just directly go into the model package and easily find what you are looking for. If you do no have such a structure, it becomes a time-consuming problem in large projects.

So, let’s start with what type of packages we can have in a regular Spring Boot application and then look at the complete structure.

1- Model classes (model), one of the basic types we can see in almost every application. These classes represents data objects.

2- Service classes (service), services are important parts of a Spring Boot application and they hold business logic. They will go under this package.

3- Repository interfaces (repository), repositories provides data access abstraction and such interfaces especially the ones extending various Repository interfaces will go under this. You may also use Data Access Objects and you can put them in a package called dao.

4- Controller classes (controller), controllers are responsible for handling incoming requests and redirecting them to a suitable service.

5- Configuration classes (configuration), every configuration classes can go under this. SecurityConfiguration can be of them if you apple security in your application.

6- Exceptions (exception), a seperate package for custom exceptions is a must-have as they are from different context. One example could be UsernameAlreadyExistsException if you have sign-up logic in your app.

7- DTO (Data transfer object) classes (dto) , DTO’s exist for carrying data. They are different than model classes as DTO’S are specialized for transferring data. For example, UserDTO class holds all the fields of User model except sensitive fields such as password.

8- Util classes (util), these are the classes that have utility methods used accross application. One example could be JWTUtil where you put your jwt related methods such as validation.

There are also some you may need according to your needs.

9- Validator classes (validator), all classes created for validation logic go under this package.

10- Advice classes (advice), these are the classes that is used to catch thrown exceptions across the application and then return custom response. They belong to this package.

You should also be aware of sub packaging. In a package, there may need a sub packages to seperate classes according to their context. For example, under exception package, User and Document exceptions must be in the same package as they are both exceptions but belong to different context. See the below example

So, finally we can look at the possible complete packet structure of a regular Spring Boot application. (It may be changed according to needs as I said)

In this tutorial, I talked about project structure and packaging classes in a proper way. I hope you like it.

--

--

Ömer Kurular

I am Ömer and currently working as a full-time software engineer. I will share my knowledge with you I gained through years of professional and self working.