↓Skip to main content

The package structure in a Spring Boot application

·2 mins

In an application developed in Spring Boot we must be careful to order and structure the packages in a correct and understandable way. We will see the most standardized way to order packages in Spring Boot.

How should we order the packages in a Spring Boot application? #

There is no mandatory form that Spring imposes on us for the naming and structure of our packages. However, there are recommendations, common usage and best practices.

A good recommendation and good practice are to divide our project into layers.

We can divide our project into a package for the controllers, another one for the services, the model of our data and the access to the database.

In another article we saw components and stereotypes . We saw that Spring proposes a division into Controller, Service, _Repository proposes a division into Controller, Service, Repository to order our code.

Taking advantage of this we can create the packages for each stereotype in this way.

  • Controller package for all the endpoints that our application has.
  • Service package to add there classes that respond to the functionality and logic.
  • Model package for the representations of our data model (entities).
  • Repository package for the classes that establish the communication with the database.

Estructura paquetes

Our project would look like this.

Estructura paquetes proyecto
/

In addition to these packages for our classes we see other folders for the resources, and the package for the tests. In the resources folder we place the properties file.

Conclusion #

We saw here the usual and classic distribution in the ordering of packages. Dividing packages into layers is the most commonly used application.