spring-boot-project-creator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Spring 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项目)
  • DockerDocker Compose:用于运行本地基础设施服务
  • curlunzip:用于从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:
ParameterDefaultOptions
Group ID
com.example
Any valid Java package name
Artifact ID
demo
Kebab-case identifier
Package NameSame as Group IDValid Java package
Spring Boot Version
3.4.5
3.4.x
,
4.0.x
(check start.spring.io for latest)
Java Version
21
17
,
21
ArchitectureUser choice
DDD
or
Layered
Docker ServicesUser choicePostgreSQL, Redis, MongoDB (multi-select)
Build Tool
maven
maven
,
gradle
使用AskUserQuestion向用户收集以下项目参数,并提供合理默认值:
参数默认值可选值
Group ID
com.example
任何合法的Java包名
Artifact ID
demo
短横线分隔式标识符
Package Name与Group ID相同合法的Java包名
Spring Boot版本
3.4.5
3.4.x
4.0.x
(可查看start.spring.io获取最新版本)
Java版本
21
17
21
架构风格用户选择
DDD
Layered
Docker服务用户选择PostgreSQL、Redis、MongoDB(可多选)
构建工具
maven
maven
gradle

2. Generate Project with Spring Initializr

2. 通过Spring Initializr生成项目

Use
curl
to download the project scaffold from start.spring.io.
Base dependencies (always included):
  • web
    — Spring Web MVC
  • validation
    — Jakarta Bean Validation
  • data-jpa
    — Spring Data JPA
  • testcontainers
    — Testcontainers support
Conditional dependencies (based on Docker Services selection):
  • PostgreSQL selected → add
    postgresql
  • Redis selected → add
    data-redis
  • MongoDB selected → add
    data-mongodb
bash
undefined
使用
curl
从start.spring.io下载项目脚手架。
基础依赖(始终包含):
  • web
    — Spring Web MVC
  • validation
    — Jakarta Bean Validation
  • data-jpa
    — Spring Data JPA
  • testcontainers
    — Testcontainers支持
条件依赖(根据Docker服务选择):
  • 选择PostgreSQL → 添加
    postgresql
  • 选择Redis → 添加
    data-redis
  • 选择MongoDB → 添加
    data-mongodb
bash
undefined

Example 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
unzip -o starter.zip -d ./demo rm starter.zip cd demo
undefined
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
unzip -o starter.zip -d ./demo rm starter.zip cd demo
undefined

3. Add Additional Dependencies

3. 添加额外依赖

Edit
pom.xml
to add SpringDoc OpenAPI and ArchUnit for architectural testing.
xml
<!-- 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>
编辑
pom.xml
以添加SpringDoc OpenAPI和用于架构测试的ArchUnit。
xml
<!-- 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 @ControllerAdvice
Create placeholder classes for each layer:
  • config/OpenApiConfig.java — SpringDoc OpenAPI configuration bean
  • exception/GlobalExceptionHandler.java
    @RestControllerAdvice
    with standard error handling
  • 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/          # @RestControllerAdvice
Create placeholder classes for each layer:
  • infrastructure/config/OpenApiConfig.java — SpringDoc OpenAPI configuration bean
  • presentation/exception/GlobalExceptionHandler.java
    @RestControllerAdvice
    with standard error handling
  • 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
src/main/resources/application.properties
with the selected services.
Always include:
properties
undefined
创建
src/main/resources/application.properties
并配置所选服务。
始终包含的配置:
properties
undefined

Application

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:**

```properties
springdoc.swagger-ui.doc-expansion=none springdoc.swagger-ui.operations-sorter=alpha springdoc.swagger-ui.tags-sorter=alpha

**如果选择了PostgreSQL:**

```properties

PostgreSQL / 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:**

```properties
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

**如果选择了Redis:**

```properties

Redis

Redis

spring.data.redis.host=localhost spring.data.redis.port=6379 spring.data.redis.password=${REDIS_PASSWORD:changeme}

**If MongoDB is selected:**

```properties
spring.data.redis.host=localhost spring.data.redis.port=6379 spring.data.redis.password=${REDIS_PASSWORD:changeme}

**如果选择了MongoDB:**

```properties

MongoDB

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}
undefined
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}
undefined

6. Set Up Docker Compose

6. 设置Docker Compose

Create
docker-compose.yaml
at the project root with only the services the user selected.
yaml
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.yaml
,仅包含用户选择的服务。
yaml
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

7. Create
.env
File for Docker Compose

7. 为Docker Compose创建.env文件

Create a
.env
file at the project root with default credentials for local development:
env
undefined
在项目根目录创建
.env
文件,包含本地开发的默认凭据:
env
undefined

PostgreSQL

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
.env
file to
.gitignore
:
undefined
将Docker Compose卷目录和.env文件追加到.gitignore:
undefined

Docker Compose

Docker Compose

.env postgres_data/ redis_data/ mongo_data/
undefined
.env postgres_data/ redis_data/ mongo_data/
undefined

9. Verify the Build

9. 验证构建

Run the Maven build to confirm the project compiles and tests pass:
bash
./mvnw clean verify
If 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.html

Architecture Patterns

架构模式

Layered Architecture

分层架构

Traditional three-tier architecture with clear separation of concerns:
LayerPackageResponsibility
Presentation
controller/
HTTP endpoints, request/response mapping
Business
service/
Business logic, transaction management
Data Access
repository/
Database operations via Spring Data
Domain
model/
JPA entities and DTOs
Best for: Simple CRUD applications, small-to-medium services, teams new to Spring Boot.
传统的三层架构,关注点清晰分离:
层级职责
表现层
controller/
HTTP端点、请求/响应映射
业务层
service/
业务逻辑、事务管理
数据访问层
repository/
通过Spring Data执行数据库操作
领域层
model/
JPA实体和DTO
适用场景: 简单CRUD应用、中小型服务、刚接触Spring Boot的团队。

DDD Architecture

DDD架构

Domain-Driven Design with hexagonal boundaries:
LayerPackageResponsibility
Domain
domain/
Entities, value objects, domain services (framework-free)
Application
application/
Use cases, orchestration, DTO mapping
Infrastructure
infrastructure/
JPA adapters, external integrations, configuration
Presentation
presentation/
REST controllers, error handling
Best for: Complex business domains, microservices with rich logic, long-lived projects.
采用六边形边界的领域驱动设计:
层级职责
领域层
domain/
实体、值对象、领域服务(无框架依赖)
应用层
application/
用例、编排、DTO映射
基础设施层
infrastructure/
JPA适配器、外部集成、配置
表现层
presentation/
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.zip
Result: Layered project with
controller/
,
service/
,
repository/
,
model/
packages, PostgreSQL Docker Compose, and SpringDoc OpenAPI.
用户请求: "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
结果: 包含
controller/
service/
repository/
model/
包的分层项目,带有PostgreSQL Docker Compose和SpringDoc OpenAPI。

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.zip
Result: DDD project with
domain/
,
application/
,
infrastructure/
,
presentation/
packages, PostgreSQL + Redis Docker Compose, and SpringDoc OpenAPI.
用户请求: "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
结果: 包含
domain/
application/
infrastructure/
presentation/
包的DDD项目,带有PostgreSQL+Redis Docker Compose和SpringDoc OpenAPI。

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
    docker-compose.yaml
    to avoid unexpected breaking changes.
  • Run
    ./mvnw clean verify
    after setup to ensure everything compiles and tests pass.
  • Add Testcontainers for integration tests instead of relying on Docker Compose.
  • 始终使用Spring Initializr生成项目,以获取正确的依赖管理和父POM。
  • 使用Java record作为DTO——它们不可变且简洁。
  • 在DDD架构中保持领域层无框架依赖——
    domain/
    包中不使用Spring注解。
  • 生产环境中使用环境变量存储敏感配置(如数据库密码等)。
  • docker-compose.yaml
    固定Docker镜像版本,避免意外的破坏性变更。
  • 设置完成后运行
    ./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
    .env
    file (git-ignored) — never commit secrets to version control.
  • The
    spring.jpa.hibernate.ddl-auto=update
    setting is for development only — use Flyway or Liquibase in production.
  • 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忽略)——切勿将机密信息提交到版本控制。
  • spring.jpa.hibernate.ddl-auto=update
    设置仅适用于开发环境——生产环境请使用Flyway或Liquibase。
  • ArchUnit版本必须与Spring Boot捆绑的JUnit 5版本兼容。