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?
-
1I have an example which I have been using for couple years. Please take a look as a reference. https://github.com/bigzidane/springboot-rest-h2-swagger – Nghia Do Jan 15 '18 at 16:29
6 Answers
From the docs:, this is the recommended way
The following listing shows a typical layout:
com
+- example
+- myapplication
+- Application.java
+- customer
+- Customer.java
+- CustomerController.java
+- CustomerService.java
+- CustomerRepository.java
+- order
+- order.java
+- OrderController.java
+- OrderService.java
+- OrderRepository. java
The Application. java file would declare the main method, along with the basic SpringBootApplication as follows:
package com.example.myapplication;
import org. springframework.boot.springApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class Application {
public static void main(string[] args)
{
springApplication.run(Application. class, args);
}
}

- 2,104
- 1
- 17
- 29

- 3,373
- 2
- 21
- 17
-
1
-
13Packages. But packages are just a fancy java word for folders that have java classes in them. – Anthony Sep 07 '20 at 05:21
-
2Surprised 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 '21 at 09:08
-
May be Author has just shown illustration purpose only, heading says `2.2. Locating the Main Application Class` – Ravi Parekh Feb 10 '21 at 09:11
-
@RaviParekh that's the reason it is recommended. We work on features not layers. It is very common with many other frameworks. – Code Name Jack Nov 23 '21 at 08:07
-
Please [don’t post images of code, error messages, documentation, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Jan 10 '22 at 06:44
-
Good article about topic: [Package by feature, not layer](http://www.javapractices.com/topic/TopicAction.do?Id=205) – Svichkarev Anatoly Feb 10 '22 at 15:03
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

- 1,297
- 8
- 9
-
6This is good for a starter projects. Still, I think that having packages such as "exceptions" and "constants" is an example of coincidental groupping, as stated here https://stackoverflow.com/questions/825281/should-exceptions-be-placed-in-a-separate-package . It also creates packages with low-to-zero cohesion, which can get problematic in larger applications. https://en.wikipedia.org/wiki/Cohesion_(computer_science) – Alkis Mavridis Sep 08 '21 at 10:22
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.

- 36,411
- 22
- 71
- 92

- 1,242
- 12
- 23
-
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? – chip Dec 01 '16 at 05:35
-
1You can follow the same structure that you are following in spring mvv.. – The Mighty Programmer Dec 01 '16 at 06: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: https://stackoverflow.com/questions/48599378/spring-boot-project-structure-with-authentication could you answer please? – Denis Stephanov Feb 03 '18 at 16:54
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

- 20,112
- 21
- 72
- 113

- 1,278
- 18
- 23
-
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
-
-
36I 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" – Reuse3733 Jul 09 '19 at 04:44
-
2Such 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](https://stackoverflow.com/a/55590796/4237346) otherwise it will result in having lots of classes in single package (or multiple subpackages). – itwasntme Dec 30 '19 at 04:06
-
3I'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](https://stackoverflow.com/a/55590796/4237346) keeps the business logic for single required functionality in single package. – itwasntme Dec 30 '19 at 04:11
-
2
-
I have a project that has a jms folder in the project structure. Can anyone explain what it is for..i know java message service but i want to know what is defined in the folder usually...i could see 2 files publisher and listener dont know what that is – Shivam... May 04 '21 at 23:52
-
Is there any tool to generate this automatically ? Such specific structure and with gradle build. – AdityaKapreShrewsburyBoston Aug 21 '21 at 14:03
-
@AdityaKapreShrewsbury Unfortunately no and personally this project structure is old now. Try reading about "Package by feature, not layer" http://www.javapractices.com/topic/TopicAction.do?Id=205 – Amar Prakash Pandey Aug 22 '21 at 17:03
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)

- 138
- 1
- 11
-
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? – chip Dec 01 '16 at 05: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 01 '16 at 07:31
-
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.

- 5,610
- 5
- 38
- 53