I'm a beginner with spring boot. I'm involved in the beginning of a project where we would build rest services using spring boot. Could you please advise the recommended directory structure to follow when building a project that will just expose rest services?
You do not need to do anything special to start. Start with a normal java project, either maven or gradle or IDE project layout with starter dependency.
You need just one main class, as per guide here and rest...
There is no constrained package structure. Actual structure will be driven by your requirement/whim and the directory structure is laid by build-tool / IDE
You can follow same structure that you might be following for a Spring MVC application.
You can follow either way
A project is divided into layers:
for example: DDD style
- Service layer : service package contains service classes
- DAO/REPO layer : dao package containing dao classes
- Entity layers
orany layer structure suitable to your problem for which you are writing problem.
A project divided into modules or functionalities or features and A module is divided into layers like above
I prefer the second, because it follows Business context. Think in terms of concepts.
What you do is dependent upon how you see the project. It is your code organization skills.
-
yes thank you, but what I would like to know is how do I package the different objects in my project? where do I place the model, services (if necessary), dao objects and impl classes? – niccolo m. Dec 1 '16 at 5:35
-
1You can follow the same structure that you are following in spring mvv.. – The Mighty Programmer Dec 1 '16 at 6:04
-
Can you tell me where should be DTO objects? In Service, in persisten layer with entities and dao/repo, in web layer with controllers and frontend, or another? – Denis Stephanov Jan 23 '18 at 23:50
-
Define a package and keep it there. Layers are just abstract concepts and help you build conceptual model of thoughts. – The Mighty Programmer Jan 24 '18 at 18:24
-
@theBeacon thanks, and what about security? split files or store in separated folder? here is my question: stackoverflow.com/questions/48599378/… could you answer please? – Denis Stephanov Feb 3 '18 at 16:54
config - class which will read from property files
cache - caching mechanism class files
constants - constant defined class
controller - controller class
exception - exception class
model - pojos classes will be present
security - security classes
service - Impl classes
util - utility classes
validation - validators classes
bootloader - main class
From the docs:, this is the recommended way
-
-
1Packages. But packages are just a fancy java word for folders that have java classes in them. – Anthony Sep 7 '20 at 5:21
-
Surprised many vote, With this structure package can be extended department/module wise, also looks bit different that what we normally use eg. @Subhasish answer, Again it is developer/team preference – Ravi Parekh Feb 10 at 9:08
-
May be Author has just shown illustration purpose only, heading says
2.2. Locating the Main Application Class
– Ravi Parekh Feb 10 at 9:11
Though this question has an accepted answer, still I would like to share my project structure for RESTful services.
src/main/java
+- com
+- example
+- Application.java
+- ApplicationConstants.java
+- configuration
| +- ApplicationConfiguration.java
+- controller
| +- ApplicationController.java
+- dao
| +- impl
| | +- ApplicationDaoImpl.java
| +- ApplicationDao.java
+- dto
| +- ApplicationDto.java
+- service
| +- impl
| | +- ApplicationServiceImpl.java
| +- ApplicationService.java
+- util
| +- ApplicationUtils.java
+- validation
| +- impl
| | +- ApplicationValidationImpl.java
| +- ApplicationValidation.java
-
1I like your answer and how it's presented. I almost always have a repository or db package in addition to the ones you listed – kyleus Mar 26 '19 at 17:59
-
-
12I would strongly suggest against the MyClassImpl and MyClass pattern. Abstraction should be introduced when it is needed. Therefore start with a concrete implementation MyClass and if/ever needed you can always extract an interface later and give proper names to the specific implementation other than "Impl" – berardino Jul 9 '19 at 4:44
-
1Such structure seems to be good, for some time... For configuration, util and maybe validation classes it can have this structure, but for others, when having lots business use cases it's much better to go with the docs structure shown in this answer otherwise it will result in having lots of classes in single package (or multiple subpackages). – itwasntme Dec 30 '19 at 4:06
-
2I'm currently dealing with kind of
legacy project
having above structure and it makes it hard to develop new functionalities. It contains of over hundred controllers, services and others, which makes the packages unreadable and requires to search through whole project to maintain convention. Having structure like this keeps the business logic for single required functionality in single package. – itwasntme Dec 30 '19 at 4:11
I have an example which I have been using for couple years. Please take a look as a reference.
There is a somehow recommended directory structure mentioned at https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html
You can create a api
folder and put your controllers there.
If you have some configuration beans, put them in a separate package too.
Use Link-1 to generate a project. this a basic project for learning. you can understand the folder structure. Use Link-2 for creating a basic Spring boot project. 1: http://start.spring.io/ 2: https://projects.spring.io/spring-boot/
Create a gradle/maven project Automatically src/main/java and src/main/test will be created. create controller/service/Repository package and start writing the code.
-src/main/java(source folder) ---com.package.service(package) ---ServiceClass(Class) ---com.package.controller(package) ---ControllerClass(Class)
-
yes thank you, but what I would like to know is how do I package the different objects in my project? where do I place the model, services (if necessary), dao objects and impl classes? – niccolo m. Dec 1 '16 at 5:35
-
Yes, if you are write any services put it under services package. Structure will look something like this. -src/main/java(source folder) – Rahul Kumar Dec 1 '16 at 7:31
-
I think this is a good structure. And it is a nicely written blog explaining the mindset of these choices.
Please use Spring Tool Suite (Eclipse-based development environment that is customized for developing Spring applications).
Create a Spring Starter Project, it will create the directory structure for you with the spring boot maven dependencies.