[JAVA] Hello World with Spring Boot

We are planning to develop with SpringBoot + Thymeleaf + Gradle at the next site, For both rabbits and horns, start with Hello World!

So, at first, I will simply use StringBoot and describe the procedure to display Hello World in HTML.

Development environment

OS:windows10 Pro JDK:11.0.3 IDE: STS (Spring Tool Suite) 3.9.8.RELEASE * Hereinafter referred to as STS. Build tool: Gradle 3.X

Execution procedure

1. Creation of "Spring Starter Project"

First, create a project named "Hello-sample" by following the steps below.

** 1-1. ** Select [File (or right-click on the project folder)]> [New]> [Spring Starter Project] from the STS menu.

** 1-2. ** On the screen below, enter "hello-sample" as the name, select "Gradle (Buildship 3.x)" as the type, select the Java version to use, and click the "Next" button. image.png

** 1-3. ** On the screen below, select "Web" and "Thymeleaf" and click the "Finish" button.

When you create a new project, you should see the following structure. image.png

** 1-4. ** Right-click on the project and select Run> Spring Boot Application.

The following log is output to the STS console. If there are no errors, the "Spring Starter Project" has been created successfully. image.png

2. Creating a screen (HTML)

Create the screen to be displayed next, that is, HTML.

** 2-1. ** Select File (or right-click on the project folder)> New> Other from the STS menu.

** 2-2. ** On the displayed screen, enter "html" in the wizard field and apply a filter. "HTML file" will be displayed. Select this and click the "Next" button. image.png

** 2-3. ** Specify the location and file name to create the HTML file, and click the "Finish" button. -Specify the location to create: Enter the following path in [Enter or select parent folder].    hello-sample/src/main/resources/templates -Specify file name: Enter "index.html" in [File name].

image.png

** 2-4. ** Change the created HTML file so that it has the following contents.

index.html


<!DOCTYPE html>
<html>
  <head>
    <title>Hello</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>Hello SpringBoot Sample</h1>
  </body>
</html>

3. 3. Creating a Controller class

I have prepared the HTML to display, but it cannot be displayed as it is. Create a Controller class that will be "C" in MVC.

** 3-1. ** From the STS menu, select [File (or right-click on the project folder)]> [New]> [Class] and create a class with the name "HelloController". image.png

I think it will be configured like this. image.png

** 3-2. ** Change the created Controller class so that it has the following contents.

HelloController.java


package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        return "index";  //Specify the name of the HTML file to be displayed (no extension required)
    }		
}

4. Run

Once this is done, right-click on the project> Run> Spring Boot Application to run it. After starting, try hitting the following URL from your browser.    URL:http://localhost:8080/

The following screen will be displayed. image.png

that's all. In the next article, I'd like to pass a value from the Controller class.

Recommended Posts

Hello World with Spring Boot!
Hello World with Spring Boot
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
(IntelliJ + gradle) Hello World with Spring Boot
Hello world! With Spring Boot (Marven + text editor)
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Spring Boot Hello World in Eclipse
Hello World (REST API) with Apache Camel + Spring Boot 2
Hello World (console app) with Apache Camel + Spring Boot 2
[Practice! ] Display Hello World in Spring Boot
How Spring Security works with Hello World
Hello World with Micronaut
Download with Spring Boot
Compare Hello, world! In Spring Boot with Java, Kotlin and Groovy
Hello World at explosive speed with Spring Initializr! !! !!
Try to display hello world with spring + gradle
Generate barcode with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Hello World with SpringBoot / Gradle
Hello, World! With Asakusa Framework!
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Hello World comparison between Spark Framework and Spring Boot
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Hello World with Docker and C
Get validation results with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
"Hello world" for ImageJ with Eclipse
I tried Flyway with Spring Boot
Hello World with GWT 2.8.2 and Maven
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Create a Hello World web app with Spring framework + Jetty
Processing at application startup with Spring Boot
Send regular notifications with LineNotify + Spring Boot