create-spring-boot-java-project

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Create Spring Boot Java project prompt

创建Spring Boot Java项目指引

  • Please make sure you have the following software installed on your system:
    • Java 21
    • Docker
    • Docker Compose
  • If you need to custom the project name, please change the
    artifactId
    and the
    packageName
    in download-spring-boot-project-template
  • If you need to update the Spring Boot version, please change the
    bootVersion
    in download-spring-boot-project-template

Check Java version

检查Java版本

  • Run following command in terminal and check the version of Java
shell
java -version
  • 在终端运行以下命令,检查Java版本
shell
java -version

Download Spring Boot project template

下载Spring Boot项目模板

  • Run following command in terminal to download a Spring Boot project template
shell
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
  • 在终端运行以下命令下载Spring Boot项目模板
shell
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

Unzip the downloaded file

解压下载的文件

  • Run following command in terminal to unzip the downloaded file
shell
unzip starter.zip -d ./${input:projectName:demo-java}
  • 在终端运行以下命令解压下载的文件
shell
unzip starter.zip -d ./${input:projectName:demo-java}

Remove the downloaded zip file

删除下载的压缩文件

  • Run following command in terminal to delete the downloaded zip file
shell
rm -f starter.zip
  • 在终端运行以下命令删除下载的压缩文件
shell
rm -f starter.zip

Change directory to the project root

切换到项目根目录

  • Run following command in terminal to change directory to the project root
shell
cd ${input:projectName:demo-java}
  • 在终端运行以下命令切换到项目根目录
shell
cd ${input:projectName:demo-java}

Add additional dependencies

添加额外依赖

  • Insert
    springdoc-openapi-starter-webmvc-ui
    and
    archunit-junit5
    dependency into
    pom.xml
    file
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>
  • springdoc-openapi-starter-webmvc-ui
    archunit-junit5
    依赖插入到
    pom.xml
    文件中
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>

Add SpringDoc, Redis, JPA and MongoDB configurations

添加SpringDoc、Redis、JPA和MongoDB配置

  • Insert SpringDoc configurations into
    application.properties
    file
properties
undefined
  • 将SpringDoc配置插入到
    application.properties
    文件中
properties
undefined

SpringDoc configurations

SpringDoc configurations

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

- Insert Redis configurations into `application.properties` file

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

- 将Redis配置插入到`application.properties`文件中

```properties

Redis configurations

Redis configurations

spring.data.redis.host=localhost spring.data.redis.port=6379 spring.data.redis.password=rootroot

- Insert JPA configurations into `application.properties` file

```properties
spring.data.redis.host=localhost spring.data.redis.port=6379 spring.data.redis.password=rootroot

- 将JPA配置插入到`application.properties`文件中

```properties

JPA configurations

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

- Insert MongoDB configurations into `application.properties` file

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

- 将MongoDB配置插入到`application.properties`文件中

```properties

MongoDB configurations

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

Add
docker-compose.yaml
with Redis, PostgreSQL and MongoDB services

添加包含Redis、PostgreSQL和MongoDB服务的
docker-compose.yaml

  • Create
    docker-compose.yaml
    at project root and add following services:
    redis:6
    ,
    postgresql:17
    and
    mongo:8
    .
    • redis service should have
      • password
        rootroot
      • mapping port 6379 to 6379
      • mounting volume
        ./redis_data
        to
        /data
    • postgresql service should have
      • password
        rootroot
      • mapping port 5432 to 5432
      • mounting volume
        ./postgres_data
        to
        /var/lib/postgresql/data
    • mongo service should have
      • initdb root username
        root
      • initdb root password
        rootroot
      • mapping port 27017 to 27017
      • mounting volume
        ./mongo_data
        to
        /data/db
  • 在项目根目录创建
    docker-compose.yaml
    ,并添加以下服务:
    redis:6
    postgresql:17
    mongo:8
    • Redis服务需包含:
      • 密码
        rootroot
      • 端口6379映射到6379
      • ./redis_data
        挂载到
        /data
    • PostgreSQL服务需包含:
      • 密码
        rootroot
      • 端口5432映射到5432
      • ./postgres_data
        挂载到
        /var/lib/postgresql/data
    • MongoDB服务需包含:
      • 初始化根用户名
        root
      • 初始化根密码
        rootroot
      • 端口27017映射到27017
      • ./mongo_data
        挂载到
        /data/db

Add
.gitignore
file

添加
.gitignore
文件

  • Insert
    redis_data
    ,
    postgres_data
    and
    mongo_data
    directories in
    .gitignore
    file
  • .gitignore
    文件中添加
    redis_data
    postgres_data
    mongo_data
    目录

Run Maven test command

运行Maven测试命令

  • Run maven clean test command to check if the project is working
shell
./mvnw clean test
  • 运行Maven clean test命令检查项目是否正常工作
shell
./mvnw clean test

Run Maven run command (Optional)

运行Maven启动命令(可选)

  • (Optional)
    docker-compose up -d
    to start the services,
    ./mvnw spring-boot:run
    to run the Spring Boot project,
    docker-compose rm -sf
    to stop the services.
  • (可选)执行
    docker-compose up -d
    启动服务,
    ./mvnw spring-boot:run
    运行Spring Boot项目,
    docker-compose rm -sf
    停止服务。

Let's do this step by step

让我们一步步来完成