I created a Spring Boot web application with VS Code, so I will describe the procedure
$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14.3
BuildVersion: 18D109
$ java -version
openjdk version "12" 2019-03-19
OpenJDK Runtime Environment (build 12+33)
OpenJDK 64-Bit Server VM (build 12+33, mixed mode, sharing)
$ code -v
1.32.1
Install the following extensions as they are required when developing with VS Code
Java Extension Pack Spring Boot Extension Pack
After installing the extension, search for spring in the command palette and Spring Initializr: Select Generate a Maven Project
Select Java
Enter the package name
Enter the project name
Select Spring Boot version
Add web and Thymeleaf to dependencies
Spring Web
Thymeleaf
Choose a location to save your project
DemoApplication.java is created in src / main / java / com / example / sampleproject, main method is implemented
DemoApplication.java
package com.example.sampleproject;
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);
}
}
Create a controller folder in src / main / java / com / example / sampleproject and Create SampleController.java in it
SampleController.java
package com.example.sampleproject.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SampleController {
@RequestMapping("/sample")
public String sample() {
return "sample";
}
}
Create sample.html in src / main / resources / templates
sample.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample</title>
</head>
<body>
<h1>HelloWorld</h1>
</body>
</html>
Debug → Select Start Debugging and select Java in the environment selection.
Since launch.json is generated, select Debug again → Start debugging
Because the Spring Boot application launches on the local server HTML content is displayed when connecting to the following URL
http://localhost:8080/sample
Recommended Posts