spring-boot-project-creator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSpring Boot Project Creator
Spring Boot 项目生成工具
Overview
概述
Generates a fully configured Spring Boot project from scratch using the Spring Initializr API. The skill walks the user through selecting project parameters, choosing an architecture style (DDD or Layered), configuring data stores, and setting up Docker Compose for local development. The result is a build-ready project with standardized structure, dependency management, and configuration.
通过Spring Initializr API从零开始生成一个完全配置好的Spring Boot项目。该工具会引导用户选择项目参数、架构风格(DDD或分层)、配置数据存储,并为本地开发设置Docker Compose。最终生成的项目具备标准化结构、依赖管理和配置,可直接进行构建。
When to Use
适用场景
- Bootstrap a new Spring Boot 3.x or 4.x project with a standard structure.
- Initialize a backend microservice with JPA, SpringDoc OpenAPI, and Docker Compose.
- Scaffold a project following either DDD (Domain-Driven Design) or Layered (Controller/Service/Repository/Model) architecture.
- Set up local development infrastructure with PostgreSQL, Redis, and/or MongoDB via Docker Compose.
- Trigger phrases: "create spring boot project", "new spring boot app", "bootstrap java project", "scaffold spring boot microservice", "initialize spring boot backend", "generate spring boot project".
- 搭建具备标准结构的Spring Boot 3.x或4.x新项目
- 初始化包含JPA、SpringDoc OpenAPI和Docker Compose的后端微服务
- 按照DDD(领域驱动设计)或分层(Controller/Service/Repository/Model)架构搭建项目
- 通过Docker Compose搭建包含PostgreSQL、Redis和/或MongoDB的本地开发基础设施
- 触发关键词:"create spring boot project"、"new spring boot app"、"bootstrap java project"、"scaffold spring boot microservice"、"initialize spring boot backend"、"generate spring boot project"
Prerequisites
前置条件
Before starting, ensure the following tools are installed:
- Java Development Kit (JDK): Version 17+ (Java 21 recommended for Spring Boot 3.x/4.x)
- Apache Maven: Build tool (Spring Initializr generates Maven projects by default)
- Docker and Docker Compose: For running local infrastructure services
- curl and unzip: For downloading and extracting the project from Spring Initializr
开始操作前,请确保已安装以下工具:
- Java Development Kit (JDK):17+版本(Spring Boot 3.x/4.x推荐使用Java 21)
- Apache Maven:构建工具(Spring Initializr默认生成Maven项目)
- Docker 和 Docker Compose:用于运行本地基础设施服务
- curl 和 unzip:用于从Spring Initializr下载并解压项目
Instructions
操作步骤
Follow these steps to create a new Spring Boot project.
按照以下步骤创建新的Spring Boot项目。
1. Gather Project Configuration
1. 收集项目配置信息
Ask the user for the following project parameters using AskUserQuestion. Provide sensible defaults:
| Parameter | Default | Options |
|---|---|---|
| Group ID | | Any valid Java package name |
| Artifact ID | | Kebab-case identifier |
| Package Name | Same as Group ID | Valid Java package |
| Spring Boot Version | | |
| Java Version | | |
| Architecture | User choice | |
| Docker Services | User choice | PostgreSQL, Redis, MongoDB (multi-select) |
| Build Tool | | |
使用AskUserQuestion向用户收集以下项目参数,并提供合理默认值:
| 参数 | 默认值 | 可选值 |
|---|---|---|
| Group ID | | 任何合法的Java包名 |
| Artifact ID | | 短横线分隔式标识符 |
| Package Name | 与Group ID相同 | 合法的Java包名 |
| Spring Boot版本 | | |
| Java版本 | | |
| 架构风格 | 用户选择 | |
| Docker服务 | 用户选择 | PostgreSQL、Redis、MongoDB(可多选) |
| 构建工具 | | |
2. Generate Project with Spring Initializr
2. 通过Spring Initializr生成项目
Use to download the project scaffold from start.spring.io.
curlBase dependencies (always included):
- — Spring Web MVC
web - — Jakarta Bean Validation
validation - — Spring Data JPA
data-jpa - — Testcontainers support
testcontainers
Conditional dependencies (based on Docker Services selection):
- PostgreSQL selected → add
postgresql - Redis selected → add
data-redis - MongoDB selected → add
data-mongodb
bash
undefined使用从start.spring.io下载项目脚手架。
curl基础依赖(始终包含):
- — Spring Web MVC
web - — Jakarta Bean Validation
validation - — Spring Data JPA
data-jpa - — Testcontainers支持
testcontainers
条件依赖(根据Docker服务选择):
- 选择PostgreSQL → 添加
postgresql - 选择Redis → 添加
data-redis - 选择MongoDB → 添加
data-mongodb
bash
undefinedExample for Spring Boot 3.4.5 with PostgreSQL only
Example for Spring Boot 3.4.5 with PostgreSQL only
curl -s https://start.spring.io/starter.zip
-d type=maven-project
-d language=java
-d bootVersion=3.4.5
-d groupId=com.example
-d artifactId=demo
-d packageName=com.example
-d javaVersion=21
-d packaging=jar
-d dependencies=web,data-jpa,postgresql,validation,testcontainers
-o starter.zip
-d type=maven-project
-d language=java
-d bootVersion=3.4.5
-d groupId=com.example
-d artifactId=demo
-d packageName=com.example
-d javaVersion=21
-d packaging=jar
-d dependencies=web,data-jpa,postgresql,validation,testcontainers
-o starter.zip
unzip -o starter.zip -d ./demo
rm starter.zip
cd demo
undefinedcurl -s https://start.spring.io/starter.zip
-d type=maven-project
-d language=java
-d bootVersion=3.4.5
-d groupId=com.example
-d artifactId=demo
-d packageName=com.example
-d javaVersion=21
-d packaging=jar
-d dependencies=web,data-jpa,postgresql,validation,testcontainers
-o starter.zip
-d type=maven-project
-d language=java
-d bootVersion=3.4.5
-d groupId=com.example
-d artifactId=demo
-d packageName=com.example
-d javaVersion=21
-d packaging=jar
-d dependencies=web,data-jpa,postgresql,validation,testcontainers
-o starter.zip
unzip -o starter.zip -d ./demo
rm starter.zip
cd demo
undefined3. Add Additional Dependencies
3. 添加额外依赖
Edit to add SpringDoc OpenAPI and ArchUnit for architectural testing.
pom.xmlxml
<!-- SpringDoc OpenAPI -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.15</version>
</dependency>
<!-- ArchUnit for architecture tests -->
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>编辑以添加SpringDoc OpenAPI和用于架构测试的ArchUnit。
pom.xmlxml
<!-- SpringDoc OpenAPI -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.15</version>
</dependency>
<!-- ArchUnit for architecture tests -->
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>1.4.1</version>
<scope>test</scope>
</dependency>4. Create Architecture Structure
4. 创建架构结构
Based on the user's choice, create the package structure under .
src/main/java/<packagePath>/根据用户选择,在下创建包结构。
src/main/java/<packagePath>/Option A: Layered Architecture
选项A:分层架构
src/main/java/com/example/
├── controller/ # REST controllers (@RestController)
├── service/ # Business logic (@Service)
├── repository/ # Data access (@Repository, Spring Data interfaces)
├── model/ # JPA entities (@Entity)
│ └── dto/ # Request/Response DTOs (Java records)
├── config/ # Configuration classes (@Configuration)
└── exception/ # Custom exceptions and @ControllerAdviceCreate placeholder classes for each layer:
- config/OpenApiConfig.java — SpringDoc OpenAPI configuration bean
- exception/GlobalExceptionHandler.java — with standard error handling
@RestControllerAdvice - model/dto/ErrorResponse.java — Standard error response record
src/main/java/com/example/
├── controller/ # REST controllers (@RestController)
├── service/ # Business logic (@Service)
├── repository/ # Data access (@Repository, Spring Data interfaces)
├── model/ # JPA entities (@Entity)
│ └── dto/ # Request/Response DTOs (Java records)
├── config/ # Configuration classes (@Configuration)
└── exception/ # Custom exceptions and @ControllerAdvice为每个层创建占位类:
- config/OpenApiConfig.java — SpringDoc OpenAPI配置Bean
- exception/GlobalExceptionHandler.java — 带有标准错误处理的
@RestControllerAdvice - model/dto/ErrorResponse.java — 标准错误响应record
Option B: DDD (Domain-Driven Design) Architecture
选项B:DDD(领域驱动设计)架构
src/main/java/com/example/
├── domain/ # Core domain (framework-free)
│ ├── model/ # Entities, Value Objects, Aggregates
│ ├── repository/ # Repository interfaces (ports)
│ └── exception/ # Domain exceptions
├── application/ # Use cases / Application services
│ ├── service/ # @Service orchestration
│ └── dto/ # Input/Output DTOs (records)
├── infrastructure/ # External adapters
│ ├── persistence/ # JPA entities, Spring Data repos
│ └── config/ # Spring @Configuration
└── presentation/ # REST API layer
├── controller/ # @RestController
└── exception/ # @RestControllerAdviceCreate placeholder classes for each layer:
- infrastructure/config/OpenApiConfig.java — SpringDoc OpenAPI configuration bean
- presentation/exception/GlobalExceptionHandler.java — with standard error handling
@RestControllerAdvice - application/dto/ErrorResponse.java — Standard error response record
src/main/java/com/example/
├── domain/ # Core domain (framework-free)
│ ├── model/ # Entities, Value Objects, Aggregates
│ ├── repository/ # Repository interfaces (ports)
│ └── exception/ # Domain exceptions
├── application/ # Use cases / Application services
│ ├── service/ # @Service orchestration
│ └── dto/ # Input/Output DTOs (records)
├── infrastructure/ # External adapters
│ ├── persistence/ # JPA entities, Spring Data repos
│ └── config/ # Spring @Configuration
└── presentation/ # REST API layer
├── controller/ # @RestController
└── exception/ # @RestControllerAdvice为每个层创建占位类:
- infrastructure/config/OpenApiConfig.java — SpringDoc OpenAPI配置Bean
- presentation/exception/GlobalExceptionHandler.java — 带有标准错误处理的
@RestControllerAdvice - application/dto/ErrorResponse.java — 标准错误响应record
5. Configure Application Properties
5. 配置应用属性
Create with the selected services.
src/main/resources/application.propertiesAlways include:
properties
undefined创建并配置所选服务。
src/main/resources/application.properties始终包含的配置:
properties
undefinedApplication
Application
spring.application.name=${artifactId}
spring.application.name=${artifactId}
SpringDoc OpenAPI
SpringDoc OpenAPI
springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.operations-sorter=alpha
springdoc.swagger-ui.tags-sorter=alpha
**If PostgreSQL is selected:**
```propertiesspringdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.operations-sorter=alpha
springdoc.swagger-ui.tags-sorter=alpha
**如果选择了PostgreSQL:**
```propertiesPostgreSQL / JPA
PostgreSQL / JPA
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/${POSTGRES_DB:postgres}
spring.datasource.username=${POSTGRES_USER:postgres}
spring.datasource.password=${POSTGRES_PASSWORD:changeme}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
**If Redis is selected:**
```propertiesspring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/${POSTGRES_DB:postgres}
spring.datasource.username=${POSTGRES_USER:postgres}
spring.datasource.password=${POSTGRES_PASSWORD:changeme}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
**如果选择了Redis:**
```propertiesRedis
Redis
spring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=${REDIS_PASSWORD:changeme}
**If MongoDB is selected:**
```propertiesspring.data.redis.host=localhost
spring.data.redis.port=6379
spring.data.redis.password=${REDIS_PASSWORD:changeme}
**如果选择了MongoDB:**
```propertiesMongoDB
MongoDB
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=${MONGO_USER:root}
spring.data.mongodb.password=${MONGO_PASSWORD:changeme}
spring.data.mongodb.database=${MONGO_DB:test}
undefinedspring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=${MONGO_USER:root}
spring.data.mongodb.password=${MONGO_PASSWORD:changeme}
spring.data.mongodb.database=${MONGO_DB:test}
undefined6. Set Up Docker Compose
6. 设置Docker Compose
Create at the project root with only the services the user selected.
docker-compose.yamlyaml
services:
# Include if PostgreSQL selected
postgresql:
image: postgres:17
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
POSTGRES_DB: ${POSTGRES_DB:-postgres}
volumes:
- ./postgres_data:/var/lib/postgresql/data
# Include if Redis selected
redis:
image: redis:7
ports:
- "6379:6379"
command: redis-server --requirepass ${REDIS_PASSWORD:-changeme}
volumes:
- ./redis_data:/data
# Include if MongoDB selected
mongodb:
image: mongo:8
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER:-root}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD:-changeme}
volumes:
- ./mongo_data:/data/db在项目根目录创建,仅包含用户选择的服务。
docker-compose.yamlyaml
services:
# Include if PostgreSQL selected
postgresql:
image: postgres:17
ports:
- "5432:5432"
environment:
POSTGRES_USER: ${POSTGRES_USER:-postgres}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
POSTGRES_DB: ${POSTGRES_DB:-postgres}
volumes:
- ./postgres_data:/var/lib/postgresql/data
# Include if Redis selected
redis:
image: redis:7
ports:
- "6379:6379"
command: redis-server --requirepass ${REDIS_PASSWORD:-changeme}
volumes:
- ./redis_data:/data
# Include if MongoDB selected
mongodb:
image: mongo:8
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER:-root}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD:-changeme}
volumes:
- ./mongo_data:/data/db7. Create .env
File for Docker Compose
.env7. 为Docker Compose创建.env文件
Create a file at the project root with default credentials for local development:
.envenv
undefined在项目根目录创建文件,包含本地开发的默认凭据:
.envenv
undefinedPostgreSQL
PostgreSQL
POSTGRES_USER=postgres
POSTGRES_PASSWORD=changeme
POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD=changeme
POSTGRES_DB=postgres
Redis
Redis
REDIS_PASSWORD=changeme
REDIS_PASSWORD=changeme
MongoDB
MongoDB
MONGO_USER=root
MONGO_PASSWORD=changeme
MONGO_DB=test
Include only the variables for the services the user selected. Docker Compose automatically loads this file.MONGO_USER=root
MONGO_PASSWORD=changeme
MONGO_DB=test
仅包含用户所选服务的变量。Docker Compose会自动加载该文件。8. Update .gitignore
8. 更新.gitignore
Append Docker Compose volume directories and the file to :
.env.gitignoreundefined将Docker Compose卷目录和.env文件追加到.gitignore:
undefinedDocker Compose
Docker Compose
.env
postgres_data/
redis_data/
mongo_data/
undefined.env
postgres_data/
redis_data/
mongo_data/
undefined9. Verify the Build
9. 验证构建
Run the Maven build to confirm the project compiles and tests pass:
bash
./mvnw clean verifyIf the build succeeds, inform the user. If it fails, diagnose and fix the issue before proceeding.
运行Maven构建以确认项目可编译且测试通过:
bash
./mvnw clean verify如果构建成功,通知用户;如果失败,先诊断并修复问题再继续。
10. Present Summary to User
10. 向用户展示项目摘要
Display a summary of the created project:
Project Created Successfully
Artifact: <artifactId>
Spring Boot: <version>
Java: <javaVersion>
Architecture: <DDD | Layered>
Build Tool: Maven
Docker: <services list>
Directory: ./<artifactId>/
Next Steps:
1. cd <artifactId>
2. docker compose up -d
3. ./mvnw spring-boot:run
4. Open http://localhost:8080/swagger-ui.html显示已创建项目的摘要:
Project Created Successfully
Artifact: <artifactId>
Spring Boot: <version>
Java: <javaVersion>
Architecture: <DDD | Layered>
Build Tool: Maven
Docker: <services list>
Directory: ./<artifactId>/
Next Steps:
1. cd <artifactId>
2. docker compose up -d
3. ./mvnw spring-boot:run
4. Open http://localhost:8080/swagger-ui.htmlArchitecture Patterns
架构模式
Layered Architecture
分层架构
Traditional three-tier architecture with clear separation of concerns:
| Layer | Package | Responsibility |
|---|---|---|
| Presentation | | HTTP endpoints, request/response mapping |
| Business | | Business logic, transaction management |
| Data Access | | Database operations via Spring Data |
| Domain | | JPA entities and DTOs |
Best for: Simple CRUD applications, small-to-medium services, teams new to Spring Boot.
传统的三层架构,关注点清晰分离:
| 层级 | 包 | 职责 |
|---|---|---|
| 表现层 | | HTTP端点、请求/响应映射 |
| 业务层 | | 业务逻辑、事务管理 |
| 数据访问层 | | 通过Spring Data执行数据库操作 |
| 领域层 | | JPA实体和DTO |
适用场景: 简单CRUD应用、中小型服务、刚接触Spring Boot的团队。
DDD Architecture
DDD架构
Domain-Driven Design with hexagonal boundaries:
| Layer | Package | Responsibility |
|---|---|---|
| Domain | | Entities, value objects, domain services (framework-free) |
| Application | | Use cases, orchestration, DTO mapping |
| Infrastructure | | JPA adapters, external integrations, configuration |
| Presentation | | REST controllers, error handling |
Best for: Complex business domains, microservices with rich logic, long-lived projects.
采用六边形边界的领域驱动设计:
| 层级 | 包 | 职责 |
|---|---|---|
| 领域层 | | 实体、值对象、领域服务(无框架依赖) |
| 应用层 | | 用例、编排、DTO映射 |
| 基础设施层 | | JPA适配器、外部集成、配置 |
| 表现层 | | REST控制器、错误处理 |
适用场景: 复杂业务领域、包含丰富逻辑的微服务、长期维护的项目。
Examples
示例
Example 1: Simple REST API with PostgreSQL (Layered)
示例1:基于PostgreSQL的简单REST API(分层架构)
User request: "Create a Spring Boot project for a REST API with PostgreSQL"
bash
curl -s https://start.spring.io/starter.zip \
-d type=maven-project \
-d bootVersion=3.4.5 \
-d groupId=com.example \
-d artifactId=my-api \
-d packageName=com.example.myapi \
-d javaVersion=21 \
-d dependencies=web,data-jpa,postgresql,validation,testcontainers \
-o starter.zipResult: Layered project with , , , packages, PostgreSQL Docker Compose, and SpringDoc OpenAPI.
controller/service/repository/model/用户请求: "Create a Spring Boot project for a REST API with PostgreSQL"
bash
curl -s https://start.spring.io/starter.zip \
-d type=maven-project \
-d bootVersion=3.4.5 \
-d groupId=com.example \
-d artifactId=my-api \
-d packageName=com.example.myapi \
-d javaVersion=21 \
-d dependencies=web,data-jpa,postgresql,validation,testcontainers \
-o starter.zip结果: 包含、、、包的分层项目,带有PostgreSQL Docker Compose和SpringDoc OpenAPI。
controller/service/repository/model/Example 2: Microservice with DDD and Multiple Stores
示例2:基于DDD和多存储的微服务
User request: "Bootstrap a Spring Boot 3 microservice with DDD, PostgreSQL and Redis"
bash
curl -s https://start.spring.io/starter.zip \
-d type=maven-project \
-d bootVersion=3.4.5 \
-d groupId=com.acme \
-d artifactId=order-service \
-d packageName=com.acme.order \
-d javaVersion=21 \
-d dependencies=web,data-jpa,postgresql,data-redis,validation,testcontainers \
-o starter.zipResult: DDD project with , , , packages, PostgreSQL + Redis Docker Compose, and SpringDoc OpenAPI.
domain/application/infrastructure/presentation/用户请求: "Bootstrap a Spring Boot 3 microservice with DDD, PostgreSQL and Redis"
bash
curl -s https://start.spring.io/starter.zip \
-d type=maven-project \
-d bootVersion=3.4.5 \
-d groupId=com.acme \
-d artifactId=order-service \
-d packageName=com.acme.order \
-d javaVersion=21 \
-d dependencies=web,data-jpa,postgresql,data-redis,validation,testcontainers \
-o starter.zip结果: 包含、、、包的DDD项目,带有PostgreSQL+Redis Docker Compose和SpringDoc OpenAPI。
domain/application/infrastructure/presentation/Best Practices
最佳实践
- Always use Spring Initializr for project generation to get the correct dependency management and parent POM.
- Use Java records for DTOs — they are immutable and concise.
- Keep domain layer framework-free in DDD architecture — no Spring annotations in .
domain/ - Use environment variables for sensitive configuration in production (database passwords, etc.).
- Pin Docker image versions in to avoid unexpected breaking changes.
docker-compose.yaml - Run after setup to ensure everything compiles and tests pass.
./mvnw clean verify - Add Testcontainers for integration tests instead of relying on Docker Compose.
- 始终使用Spring Initializr生成项目,以获取正确的依赖管理和父POM。
- 使用Java record作为DTO——它们不可变且简洁。
- 在DDD架构中保持领域层无框架依赖——包中不使用Spring注解。
domain/ - 生产环境中使用环境变量存储敏感配置(如数据库密码等)。
- 在中固定Docker镜像版本,避免意外的破坏性变更。
docker-compose.yaml - 设置完成后运行,确保所有内容可编译且测试通过。
./mvnw clean verify - 添加Testcontainers用于集成测试,而非依赖Docker Compose。
Constraints and Warnings
限制与注意事项
- Spring Initializr requires internet access — this skill cannot work offline.
- Spring Boot 4.x availability depends on the current release cycle — check start.spring.io for latest versions.
- Docker Compose credentials are loaded from file (git-ignored) — never commit secrets to version control.
.env - The setting is for development only — use Flyway or Liquibase in production.
spring.jpa.hibernate.ddl-auto=update - ArchUnit version must be compatible with the JUnit 5 version bundled with Spring Boot.
- Spring Initializr需要网络连接——该工具无法离线使用。
- Spring Boot 4.x的可用性取决于当前发布周期——可查看start.spring.io获取最新版本。
- Docker Compose凭据从.env文件加载(已加入git忽略)——切勿将机密信息提交到版本控制。
- 设置仅适用于开发环境——生产环境请使用Flyway或Liquibase。
spring.jpa.hibernate.ddl-auto=update - ArchUnit版本必须与Spring Boot捆绑的JUnit 5版本兼容。