Créer un projet Spring Boot Java
-
Veuillez vous assurer d'avoir les logiciels suivants installés sur votre système :
- Java 21
- Docker
- Docker Compose
-
Si vous avez besoin de personnaliser le nom du projet, veuillez modifier l'
artifactIdet lepackageNamedans download-spring-boot-project-template -
Si vous avez besoin de mettre à jour la version de Spring Boot, veuillez modifier le
bootVersiondans download-spring-boot-project-template
Vérifier la version de Java
- Exécutez la commande suivante dans le terminal et vérifiez la version de Java
java -version
Télécharger le modèle de projet Spring Boot
- Exécutez la commande suivante dans le terminal pour télécharger un modèle de projet Spring Boot
curl https://start.spring.io/starter.zip \
-d artifactId=${input:projectName:demo-java} \
-d bootVersion=3.4.5 \
-d dependencies=lombok,configuration-processor,web,data-jpa,postgresql,data-redis,data-mongodb,validation,cache,testcontainers \
-d javaVersion=21 \
-d packageName=com.example \
-d packaging=jar \
-d type=maven-project \
-o starter.zip
Décompresser le fichier téléchargé
- Exécutez la commande suivante dans le terminal pour décompresser le fichier téléchargé
unzip starter.zip -d ./${input:projectName:demo-java}
Supprimer le fichier zip téléchargé
- Exécutez la commande suivante dans le terminal pour supprimer le fichier zip téléchargé
rm -f starter.zip
Changer de répertoire vers la racine du projet
- Exécutez la commande suivante dans le terminal pour changer de répertoire vers la racine du projet
cd ${input:projectName:demo-java}
Ajouter des dépendances supplémentaires
- Insérez la dépendance
springdoc-openapi-starter-webmvc-uietarchunit-junit5dans le fichierpom.xml
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
Ajouter les configurations SpringDoc, Redis, JPA et MongoDB
- Insérez les configurations SpringDoc dans le fichier
application.properties
# SpringDoc configurations
springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.operations-sorter=alpha
springdoc.swagger-ui.tags-sorter=alpha
- Insérez les configurations Redis dans le fichier
application.properties
# Redis configurations
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=rootroot
- Insérez les configurations JPA dans le fichier
application.properties
# JPA configurations
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=rootroot
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
- Insérez les configurations MongoDB dans le fichier
application.properties
# MongoDB configurations
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=rootroot
spring.data.mongodb.database=test
Ajouter docker-compose.yaml avec les services Redis, PostgreSQL et MongoDB
-
Créez
docker-compose.yamlà la racine du projet et ajoutez les services suivants :redis:6,postgresql:17etmongo:8.- Le service redis doit avoir
- Le mot de passe
rootroot - Le mappage du port 6379 à 6379
- Le montage du volume
./redis_dataà/data
- Le mot de passe
- Le service postgresql doit avoir
- Le mot de passe
rootroot - Le mappage du port 5432 à 5432
- Le montage du volume
./postgres_dataà/var/lib/postgresql/data
- Le mot de passe
- Le service mongo doit avoir
- Le nom d'utilisateur root initdb
root - Le mot de passe root initdb
rootroot - Le mappage du port 27017 à 27017
- Le montage du volume
./mongo_dataà/data/db
- Le nom d'utilisateur root initdb
- Le service redis doit avoir
Ajouter le fichier .gitignore
- Insérez les répertoires
redis_data,postgres_dataetmongo_datadans le fichier.gitignore
Exécuter la commande Maven test
- Exécutez la commande maven clean test pour vérifier si le projet fonctionne
./mvnw clean test
Exécuter la commande Maven run (Optionnel)
- (Optionnel)
docker-compose up -dpour démarrer les services,./mvnw spring-boot:runpour exécuter le projet Spring Boot,docker-compose rm -sfpour arrêter les services.