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.
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.
First, access spring initializr.
** 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.
Enter web.
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.
Finally, edit Project Metadata
.
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.
The Zip file will be downloaded, so
Extract the Zip file.
I'm ready.
Open the previous folder with VS Code. We recommend that you install the Java Extension Pack. It is said that you should install it.
Then open DemoApplication.java
in src / main / java / com / example / demo
.
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.
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.
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,
Hello tanaka! Is displayed. This is because the variable name contains tanaka.
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