Hello everyone. I will write an article for ** Java Advent Calendar 2020 (Qiita) Day 24 **.
I have been developing in Java for a long time, but most of the editors I use for projects are eclipse (or STS) or IntelliJ IDEA. Once you get used to InteliJ, you can choose ** InteliJ ** even if you pay the license! It was, but I suddenly touched ** Visual Studio Code ** and it feels good! It was. In this article, I would like to build a Java development environment ** with Visual Studio Code.
Also, this time, I would like to create a simple application created with "** Spring MVC, Spring Boot **" (framework), which is often used in Java development.
OS: Windows10 ** Visual Studio Code **: 1.51.1 (December 2020)
--Launch Visual Studio Code and install the extension: Java Extension Pack.
Install other extensions that you might use often.
--Display the "Command Palette" (Ctrl + Shift + p) in Visual Studio Code and execute the [** Java: Create Java Project **] command to create a Java project.
>Java: Create Java Project
No build tools
--File and directory structure of the created Java project
helloworld
├── lib
└── src
└── App.java
Compile "App.java" created by default and execute the generated class file → "Hello, World!" Is displayed.
$ javac App.java
$ java App
Hello, World!
Since you installed the "Spring Boot Extension Pack" in step 2, let's create a Spring Boot application.
--Display "** Command Palette **" (Ctrl + Shift + p) in Visual Studio Code and create a Spring boot project.
Example: com.example
5. Enter the "Input Artifact Id" project name.
```Example: demo```
6. "Specify packaging type" Package type: War this time. (Jar is OK)
7. "Specify Java type" You will be asked for the Java version to use, so select the Java version to use. Java 14 is used this time.
8. "Search for dependencies" Since it is a Maven project, you have to select dependencies. This time, the purpose is to "create a web application" and "display the screen", so select the following two.
```spring web / thymeleaf
At this point, you will be asked where to save the project. Select a location to save the project.
--The created directory has the following structure. -** DemoApplication.java **, ** ServletInitializer.java ** are implemented by default.
|― src
| |― main
| | |― java
| | | |― com
| | | |― example
| | | |― demo(Project name)
| | | |― DemoApplication.java (created by default)
| | | |― ServletInitializer.java (created by default)
| | |― resources
| | |― static/ (Static file)
| | |― templates/ (Template file)
| | |― application.properties (Environment-specific configuration file)
| |― test
| |― java
| |― com
| |― example
| |― demo(Project name)
| |― DemoApplicationTests.java
|― .gitignore
|― HELP.md
|― mvnw
|― mvnw.cmd
|― pom.xml (Maven config file)
-Create a Controller class under the \ demo \ src \ main \ java \ com \ example \ demo \ controller directory. (SampleController.java)
package com.example.demo.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 \ demo \ src \ main \ templates. (Sample.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
--Run Spring boot application Select "** SPRING-BOOT DASHBOARD **" at the bottom left of VS Code and select [start] or [debug]
--Check the display on your browser. You should see "Hello World!". http://localhost:8080/sample
It had a lot of extensions and was easier to use than I had imagined! I'm worried about continuing to use VS Code without renewing my InteliJ license ... In the future, I will strive to publish more practical Java articles.
The last day is @tkxlab!
I tried to display "Hello World" with VS Code for Spring Boot https://tech-lab.sios.jp/archives/19941
Recommended Posts