

A Guide to Creating Spring Boot Projects With Spring Initializr
source link: https://hackernoon.com/a-guide-to-creating-spring-boot-projects-with-spring-initializr
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A Guide to Creating Spring Boot Projects With Spring Initializr
A Guide to Creating Spring Boot Projects With Spring Initializr
7min
by @sammytran
Sammy Tran
@sammytran
Software engineer, writer, and foodie based out of Seattle.
Too Long; Didn't Read
Spring Boot is a popular Java framework used by companies such as Netflix, Walmart, PayPal, Udemy, and many others. You can use Spring Initializr to generate the initial Spring Boot boilerplate code by accessing it through an online form. The form creates a zip file that contains the basic project structure and configuration.
audio
element.@sammytran
Sammy TranSoftware engineer, writer, and foodie based out of Seattle.
Receive Stories from @sammytran
Credibility
A while ago, I wanted to learn how to create REST APIs with Spring Boot, a popular Java framework used by companies such as Netflix, Walmart, PayPal, Udemy, and many others. I had some experience building REST APIs with libraries like Jersey, Jetty, and Jackson, but I wanted to try Spring Boot.
You can use a tool called Spring Initializr to generate the initial Spring Boot boilerplate code by accessing it through an online form. The form creates a zip file that contains the basic project structure and configuration.
To use Spring Initializr:
- Go to start.spring.io, where you can find the Spring Initializr web tool.
- Fill out the form, specifying your desired build tool, programming language, Spring Boot version, project metadata, and dependencies.
- Click the GENERATE button at the bottom of the screen to download the autogenerated code as a zip file.
- Extract the contents of this zip file.
The Spring Initializr Form Options
At first, the many options available on the Spring Initializr form can be overwhelming. The following sections offer explanations for each form option and suggestions on which choices to make for your project.
The Project Option
The project option allows you to choose a build tool for your project. The purpose of build tools is to automate and manage the process of building software projects, which includes tasks like handling external dependencies, compiling source code, running tests, and packaging everything into distributable formats, like JARs. There are three options:
- Gradle – Groovy
- Gradle – Kotlin
- Maven
Gradle has two options because it can use a domain-specific language (DSL) based on Groovy or Kotlin. If you have used Gradle before but are still confused about the difference, you may have been using Groovy as the DSL without knowing it since it is Gradle’s original DSL.
If you do not know which option to pick, I would suggest Gradle because working with a DSL is more pleasant than XML, in my opinion. To better understand the differences between Gradle and Maven, consider reading this summary for more information.
The Language Option
The language option lets you choose a JVM-compatible programming language for your Spring Boot project. There are three options:
- Kotlin
- Groovy
If you are not familiar with Kotlin or Groovy, Java is what you want.
The Spring Boot Option
The Spring Boot option enables you to select the version of Spring Boot to use in your project. It’s important to note that Spring Boot 2.x.x and 3.x.x are distinct major versions. If you are starting a new Spring Boot project, I recommend choosing a stable 3.x.x version.
Also, note that some versions may be labeled M1, M2, RC1, or SNAPSHOT, indicating they are pre-release versions and not considered stable or production-ready. Be careful when using these versions, as they may contain bugs or incomplete features due to being in active development.
The Project Metadata Options
The project metadata options enable you to choose configuration details and settings for your project. There are seven metadata fields:
- Group – The unique identifier of your organization or project, typically written in reversed domain name notation. It helps to ensure that your package names do not conflict with packages from other organizations or projects. If you are creating a personal project, feel free to leave it as
com.example
. - Artifact – The name of the archive generated by your build tool. The artifact usually has the same value as the name field below.
- Name – The name used for the project’s root directory, as well as the default name of the main application class.
- Description – A field that provides a short description of the project.
- Package name – The name of the package generated Java classes will be placed in. The package name should be a combination of the group and name fields above (e.g.,
com.example.myproject
). - Packaging – Packaging refers to the type of archive your build tool will generate. Two common packaging types are JAR (Java Archive) and WAR (Web Archive). JAR files are usually used for standalone applications, libraries, and command-line tools, while WAR files are used for web applications intended for deployment to a web server. I do not have a general recommendation here because it depends on your use case.
- Java – The Java version for this project. If using Spring Boot version 3.x.x, the Java version must be 17+.
The Dependency Option
The dependency option allows you to select common dependencies used in Spring Boot projects that will be added to your project’s build.gradle
if you are using Gradle, or pom.xml
if you are using Maven. Some popular dependencies include:
- Lombok – Lombok provides annotations to reduce boilerplate code in Java classes.
- Spring Web – Spring Web provides functionality to build web applications, including support for RESTful web services and MVC architecture.
- Thymeleaf – Thymeleaf is a server-side template engine that allows for the creation of dynamic web pages by defining templates using HTML.
- Spring Security – Spring Security is a security framework that provides authentication, authorization, and other security features.
- Spring Data JPA – Spring Data JPA provides a simple way to interact with relational databases through the Java Persistence API (JPA) by generating boilerplate code for database operations.
You can start by adding Spring Web if you want to keep things simple. As you build your application, you can add anything else you need.
The Spring Boot Folder Structure
When you extract the zip file created by Spring Initializr, you should end up with a folder structure that looks something like this (if you chose Maven as your build tool, it will look slightly different):
.
└── demo/
└── demo/
├── .gradle/
│ └── ...
├── gradle/
│ └── ...
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── ...
│ └── test/
│ └── java/com/example/demo/
│ └── DemoApplicationTests.java
├── .gitignore
├── build.gradle
├── gradlew
├── gradlew.bat
├── HELP.md
└── settings.gradle
If you look carefully, you may notice the outer demo
folder contains another folder, also called demo
. You can remove the outer folder to simplify the folder structure to:
.
└── demo/
├── .gradle/
│ └── ...
├── gradle/
│ └── ...
├── src/
│ ├── main/
│ │ ├── java/com/example/demo/
│ │ │ └── DemoApplication.java
│ │ └── resources/
│ │ └── ...
│ └── test/
│ └── java/com/example/demo/
│ └── DemoApplicationTests.java
├── .gitignore
├── build.gradle
├── gradlew
├── gradlew.bat
├── HELP.md
└── settings.gradle
Let’s take a moment to understand what these generated files and folders are:
.gradle/
– This is where Gradle stores its project-specific cache.gradle/
– This contains the JAR file and configuration of the Gradle Wrapper.src/main/
– This contains the main source code for your application. Initially, it contains one source code file,DemoApplication.java
, the entry point for your Spring Boot project, and theresources/
directory, where configuration files, static assets, and HTML templates would be.src/test/
– This contains the test code for your application. Initially, it contains one test file,DemoApplicationTests.java
, which contains a trivial test..gitignore
– This specifies which files and folders should be ignored by Git.build.gradle
– This is the Gradle build script for your project, which defines your project’s dependencies, plugins, and other build settings.gradlew
– This is a wrapper shell script that allows you to build your Spring Boot project without already having Gradle on your system.gradlew.bat
– This is a batch script that does the same thing asgradlew
but in the Windows command prompt.HELP.md
– This is just a markdown file with some references, guides, and additional links. Feel free to delete this file.settings.gradle
– This is mostly used for multi-project builds. For your Spring Boot project, it will likely just contain the name of your project.
As you can see, most of the files and folders above are from Gradle. If you want to learn more about what these are for, I encourage you to go to the Gradle documentation.
Conclusion
Congratulations! You now have a better understanding of Spring Initializr and how to use it to create Spring Boot applications. You also have an idea of what the various files and folders autogenerated by Spring Initializr do.
Now it is time to open up your favorite text editor or IDE and start coding!
Also published here.
Recommend
-
225
Spring Boot
-
23
There has been a change to the Spring Boot Initializr. If you haven't used Spring Boot Initializr before, it is a web interface used to quickly create new Spring Boot projects. In case you don't know, Spring Boo...
-
27
Spring Boot Project-Creation-Tool, Spring Initializr, Gets Several New Updates Jul 18, 2019...
-
5
Guide to building Spring Boot library In this article, I’m going to show you how to create and share your own custom Spring Boot library. If you decided to build such a product you should follow some best practices recommend...
-
13
Handling exceptions is an important part of building a robust application. Spring Boot offers more than one way of doing it. This article will explore these ways and will also provide some pointers on when a given way might be prefera...
-
5
当我们使用Spring Initializr来创建Spring Boot工程的时候,有没有发现在工程根目录下有两个名为 mvnw 的文件:
-
14
Guide to @SpringBootTest for Spring Boot Integration Tests May 13, 2021 With this guide, we'll investigate the powerful @SpringBootTest annotation for writing integration tests. If you're...
-
6
Bean Validation is the de-facto standard for implementing validation logic in the Java ecosystem. It’s well integrated with Spring and Spring Boot. However, there are some pitfalls. This tutor...
-
5
使用 Spring Initializr 初始化 Spring Boot 项目 Spring Initializr 从本质上说就是一个Web应用程序,它能为你构建Spring Boot项目结构。 虽然不能生成应用程序代码,但它能为你提供一个基本的项目结构,以及一个用于构件代码的Maven或者Gradle构建...
-
7
Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK