Create your first Spring Boot project

Tram Ho

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:

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 , 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 , select New> Java class. Name it HomeController and enter the following code.

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.

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.

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.

Share the news now

Source : Viblo