[Java] Hello World at an explosive speed using Spring Initializr! !! !!

2 minute read

Purpose

Using Spring Boot, which is a Java framework, I want to output Hello World for the time being! I will leave it for those who say.

What is #Spring Initializr?

You can easily generate a template application by selecting a build tool or development language on the browser. (I recognize it as a scaffold in Ruby on Rails.)

Practice

Various versions handled this time

OS: macOS Mojave version 10.14.6 Text Editor: Visual Studio Code (VSCode)

$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 13.0.2, vendor: N/A, runtime: /usr/local/Cellar/openjdk/13.0.2+8_2/libexec/openjdk.jdk/Contents/Home
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
$ spring --version
Spring CLI v2.3.1.RELEASE

Go to the Spring Initializr formula Go!!!

Go to here.

Make the following additions and corrections. ![Screenshot 2020-06-21 20.36.41.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/555244/11adafb1-1e07-4ece-df36-(4d1a556d20de.png)

Then press the GENERATE button.

I think that the Zip file was downloaded by pressing the GENERATE button.

Extract the Zip file.

Fixed with a text editor

![Screenshot 2020-06-21 20.41.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/555244/f8273ea8-3314-43ae-63c7-(d07ddefdd489.png)

This time there should be a folder named demo, so open it in your text editor (this time it’s VS Code).

This is a digression, but I recommend that you install the following with the extension function (work will progress).

Java Extension Pack ・Spring Boot Tools ・Lombok Annotations Support for VS Code

Create HelloController.java in src/main/java/com/example/demo.

![Screenshot 2020-06-21 21.16.45.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/555244/4424d8dd-d5ba-d515-2c51-(9cee256a5ba5.png)

While referring to the code of Official Spring Boot , Let’s write the contents of HelloController.java!

HelloController.java


package com.example.demo;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class HelloController {
  
  @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
}

Then go to the demo folder in the terminal and Type mvn spring-boot:run.

terminal


$ mvn 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 >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using'UTF-8' encoding to copy filtered resources.

~abridgement~

After a while, the following will be displayed.

terminal


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

~abridgement~

And in the browser http://localhost:8080/ If you enter

Screenshot 2020-06-21 21.23.37.png

Hello World! is displayed!

At the end

I have omitted detailed explanations such as Spring Boot this time. (The goal was to display it on the browser)

I hope you can use it for learning such as changing the code yourself and checking it in the browser.