Hello World with Spring Boot!

Purpose

In the previous article, Hello World at explosive speed using Spring Initializr! !! !! It was an article for those who are in a hurry.

This time, I will proceed slowly by referring to the Spring Quickstart Guide.

Advance preparation

The integrated development environment is IntelliJ IDEA, Spring Tools, [Visual Studio Code](https: // code.visualstudio.com/docs/languages/java), Eclipse seems to be popular. I use Visual Studio Code (VSCode).

As for the development kit for Java development, the version of AdoptOpenJDK seems to be 8 or 11. How to install OpenJDK11 [Java development environment construction](https://qiita.com/morioheisei/items/ef8eb5d75c07b4d280f4#openjdk-11%E3%81%AE%E3%82%A4%E3%83%B3%E3 It is described in% 82% B9% E3% 83% 88% E3% 83% BC% E3% 83% AB), so if you are still, please refer to it.

1. Start the Spring Boot project!

First, access spring initializr. スクリーンショット_2020-06-29_14_37_13-2.png

** Select Maven for Project. ** **

It analyzes the source code and compiles the programming language into machine language so that the program can be executed.

Reference URL: Learn about Ant, Maven and Gradle from zero knowledge. -From build definition to tool features

** Language selects Java. ** **

** For Spring Boot, select 2.3.1. (As of June 30, 2020) **

Officially, the version changes regularly so choose the latest version. (However, do not select SNAPSHOT) is written.

Then click the ʻADD DEPENDEN CIES` button. スクリーンショット_2020-06-29_13_14_12.png

Enter web.

スクリーンショット 2020-06-29 13.30.24.png

Then select Spring Web.

What is Spring Web?

Settings for building web applications including RESTful using Spring MVC.

It is OK if it is displayed as follows. スクリーンショット_2020-06-29_14_37_13.png

Finally, edit Project Metadata.

スクリーンショット_2020-06-29_14_37_13-3.png

Group: A name that uniquely identifies the project. It is common to specify the root package name of the project. Artifact: The name of the project artifact. It seems that it is recommended to use the same name as Artifact, so if you change this, the Name will also change. Name: The display name of the project. In the case of Maven, it is also used as the class name of the main program. It seems that it is recommended to use the same name as Artifact, so changing this will change Artifact as well. Description: Enter a description for your project. Package Name: The package name of the project. Normally, it consists of the package specified by Group and the name specified by Artifact. (Group name.Artifact name) Packaging: Select either Jar or War as the packaging method.

Since we are using Java 11 this time, select 11 and click the GENERATE button.

スクリーンショット_2020-06-30_9_50_39.png

The Zip file will be downloaded, so Spring_Initializr_と_メモ.png

Extract the Zip file.

スクリーンショット 2020-06-30 10.00.39.png

I'm ready.

2. Add code!

Open the previous folder with VS Code. We recommend that you install the Java Extension Pack. It is said that you should install it.

スクリーンショット 2020-06-30 10.08.25.png

Then open DemoApplication.java in src / main / java / com / example / demo. スクリーンショット 2020-06-30 10.11.03.png

DemoApplication.java


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

@ ○○ written above the class is called an annotation.

Annotation is the meaning of annotation, which is to add related information to a certain data as annotation. It is a function to add a note to the written code in the program. By writing the annotation @SpringBootApplication, the functions required as a Spring Boot startup class will be automatically incorporated.

Add the code referring to the formula.

DemoApplication.java


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@GetMapping("/hello")
	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return String.format("Hello %s!", name);
	}
}

By writing the annotation @RestController, the class will be recognized as a Controller. Since it does not transition to View, the return value of the method is written to the response body as it is. (This time, Hello 〇〇! Is returned)

By writing the annotation @GetMapping, the method will be called when accessed by that URL. This time, the hello method is called when http: // localhost8080 / hello is accessed.

By writing the annotation @RequestParam, you can receive the query parameters of the URL. This time, if you access? Name = 〇〇, the value entered in 〇〇 will be stored in the variable name, and if nothing is specified, World will be stored.

3. Let's run it!

In your terminal, go to the folder that contains this project file. Then enter ./mvnw spring-boot: run to run it.

Terminal


$ ./mvnw spring-boot:run

[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:2.3.1.RELEASE:run (default-cli) > test-compile @ demo >>>

~abridgement~

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-06-30 11:41:46.106  INFO 64839 --- [           main] com.example.demo.DemoApplication         : Starting Demo

~abridgement~

Is displayed, it is successful!

Try accessing http: // localhost: 8080 / hello.

スクリーンショット 2020-06-30 11.47.12.png

Hello World! Is displayed. This is because the variable name contains World.

Next, try adding /? name= arbitrary string after the hello in the URL, as shown below. http://localhost:8080/hello/?name=tanaka

And when you press Enter, スクリーンショット 2020-06-30 11.50.38.png

Hello tanaka! Is displayed. This is because the variable name contains tanaka.

At the end

This time, I was able to proceed slowly with reference to the formula and create the foundation of the Web application. We hope that you can use it for learning, such as changing the code yourself and checking it with a browser.

Recommended Posts

Hello World with Spring Boot
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
[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
Hello World with VS Code!
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
Create an app with Spring Boot 2
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Inquiry application creation with Spring Boot
Hello world with Kotlin and JavaFX
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
Hello world with Java template engine Thymeleaf