1. Prepare
For Spring Boot, you can choose one of two IDEs, Eclipse (free) and IntelliJ IDEA Ultimate (Community version does not have Spring support). Download it here:
- Eclipse: https://www.eclipse.org/downloads/
- IntelliJ IDEA: https://www.jetbrains.com/idea/download/
I encourage you to use IntelliJ, because the code is very happy and there are few errors like Eclipse. With IntelliJ, there are already plugins available, no need to install much. And Eclipse you need to install STS (Spring tool suite). You can download the JAR here, or look in the Eclipse Marketplace.
In the next part, I will use IntelliJ to demo.
2. Spring initializr
Spring Boot has a tool that helps us to quickly initialize a project called Spring Initializr. Spring Initializr can be accessed on the web at http://start.spring.io/ , or with IntelliJ, it is always integrated into the project creation.
2.1. Report project information
As shown above, in the left pane is where we declare some project information such as:
- Project type: choose what kind of package manager, Maven or Gradle.
- Language: choose the code language, here I choose Java
- Spring Boot Version: The versions with SNAPSHOT are not stable, should not be selected
- Type of build file: with Spring Boot, you should choose JAR to help configure Tomcat server
- Java version: choose java 11 for stability
In addition, it is also necessary to declare more metadata such as project name, package name, artifact, …
2.2. Choose the dependency
The right pane is to select dependencies, which can be understood as backend libraries. To code a web service you need a Spring web. Other libraries have the following meanings:
- Lombok: should be selected, it makes the Java code shorter, but needs to install the Lombok plugin in the IDE as well
- Thymeleaf: not needed, Thymeleaf will help pass data into view of MVC model, return HTML page with data to client
- Spring configuration processor, Spring devtools are tools that add support when coding
In this section, I encourage you to choose from: Spring Web, Lombok, Thymeleaf.
2.3. Completed
Once done, you press the Generate button to finish. A zip file containing the original source will be downloaded, just unzip and start coding.
The project structure is pre-initialized as follows.
3. Simple program code
This section will guide you to create a web page that returns simple HTML when the user visits.
B1. Simple Controller creation
The controller is the first component that captures the URL that the user visits. For example, if you go to web home page, the controller method with mapping to URL / will be called.
You right-click on the left entry named com.abc.xyz
, select New> Java class. Name it HomeController
and enter the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token keyword">package</span> <span class="token namespace">com <span class="token punctuation">.</span> example <span class="token punctuation">.</span> demo</span> <span class="token punctuation">;</span> <span class="token keyword">import</span> <span class="token namespace">org <span class="token punctuation">.</span> springframework <span class="token punctuation">.</span> web <span class="token punctuation">.</span> bind <span class="token punctuation">.</span> annotation</span> <span class="token punctuation">.</span> <span class="token class-name">GetMapping</span> <span class="token punctuation">;</span> <span class="token annotation punctuation">@Controller</span> <span class="token comment">// Chỉ định HomeController là Controller</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HomeController</span> <span class="token punctuation">{</span> <span class="token comment">// Khi user truy cập vào endpoint / thì homepage() được gọi</span> <span class="token annotation punctuation">@GetMapping</span> <span class="token punctuation">(</span> <span class="token string">"/"</span> <span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">homepage</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token string">"index"</span> <span class="token punctuation">;</span> <span class="token comment">// Trả về trang index.html</span> <span class="token punctuation">}</span> <span class="token comment">// Có thể mapping thêm các endpoint khác nữa...</span> <span class="token punctuation">}</span> |
This web is quite simple, only one endpoint is / (homepage). Spring Boot uses @Annotation
to specify the meaning of some elements in the code, in my opinion it is quite good and makes the code easier to understand.
B2. Creates the HTML page to render
As above we have HomeController
, when accessing /
will return the index.html
page. Right-click on the resources/template
directory, select New > HTML file
and type the name index.html
.
If you do not know the HTML, then type the following, if you know it, you can freely customize it.
1 2 3 4 5 6 7 8 9 10 11 | <span class="token doctype"><!DOCTYPE html></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> html</span> <span class="token attr-name">lang</span> <span class="token attr-value"><span class="token punctuation">=</span> <span class="token punctuation">"</span> en <span class="token punctuation">"</span></span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> head</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> meta</span> <span class="token attr-name">charset</span> <span class="token attr-value"><span class="token punctuation">=</span> <span class="token punctuation">"</span> UTF-8 <span class="token punctuation">"</span></span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> title</span> <span class="token punctuation">></span></span> Spring Boot web <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> title</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> head</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> body</span> <span class="token punctuation">></span></span> Hello World! <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> body</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> html</span> <span class="token punctuation">></span></span> |
This step you need to pay attention to install the Thymeleaf library, otherwise it will not recognize.
B3. Run and see the results
Press the green button in the top right of the IDE to run the program. The startup depends on the project size, if the project is empty, it will start up very quickly.
Because you have not configured the port for the web, Spring Boot will by default get port 8080 to run, or another empty port on your computer. You can see in the IDE console which port the web is running (line Tomcat started on port(s): 8080
)
Then open a browser and go to localhost:8080
or replace it with your port. The result is here.
4. Run the program
4.1. Runs in the IDE
This part doesn’t say anything, it’s too basic. Just press one of the two blue buttons above right
4.2. Build to JAR file
To build into JAR files, you need to run the tasks corresponding to the selected package manager. If the project using Maven needs to run the task jar , Gradle is the bootJar .
After waiting for a while, Maven or Gradle will build the directory containing the class and JAR files:
- Maven is the target directory, right inside there is a JAR file
- Gradle is the build directory, the JAR file is located inside the libs subdirectory
4.3. Run the JAR file
After you have the JAR file, you use the following command to run. The run is similar to Run in the IDE.
1 2 | java -jar ABC.jar |
This JAR file can be run anywhere running java, on your computer or the server running the same. The reason is because inside the JAR file, the Tomcat server is already embedded.