[Java] (Intellij)Spring BootでHelloWorld

less than 1 minute read

Procedure

  1. In Intel iJ, select “File” -> “New” -> “Project”
  2. In the displayed New Project, select “Spring Initializer” and click “Next”.
  3. Enter “Group” and “Artifact” appropriately. Since we want to deploy to the server, select “War” for “Packaging” and click “Next”.
  4. I want to create a web application, so check “Web” → “Spring Web” and click “Next”.
  5. Enter “Project name” and “Project location” appropriately and click “Fisnish”
  6. Add the following to the automatically created DemoApplication and execute.

I was addicted to the lack of @RestController

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.RestController;

@SpringBootApplication
+ @RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
+ @GetMapping("/hello")
+ public String sayHello() {
+ return "Hello World";
+}
}

  1. When you access the URL below, “Hello World” should be displayed

http://localhost:8080/hello

 Screenshot 2020-07-26 12.53.06.png