Introduction to Java development environment & Spring Boot application created with VS Code

Hello everyone. I will write an article for ** Java Advent Calendar 2020 (Qiita) Day 24 **.

Introduction

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.

Assumed development environment

OS: Windows10 ** Visual Studio Code **: 1.51.1 (December 2020)

Procedure (3 steps)

  1. Java Extension Pack installation
  2. Let's run Java!
  3. Let's create a Spring Boot application!

Step 1: Install Java Extension Pack

--Launch Visual Studio Code and install the extension: Java Extension Pack.

Install other extensions that you might use often.

Step 2: Let's run Java!

--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!

Step 3: Let's create a Spring Boot application!

Since you installed the "Spring Boot Extension Pack" in step 2, let's create a Spring Boot application.

Creating a project

--Display "** Command Palette **" (Ctrl + Shift + p) in Visual Studio Code and create a Spring boot project.

Operation on the command palette

  1. Select "Spring Initializr: Generate a Maven Project"
  2. "Specify Spring Boot version" Select the version of Spring Boot. Since there is no particular designation this time, select the latest (2.4.0) as of 2020/12
  3. "Specify project language" Select the language to use. Select Java this time
  4. Enter the "Input Group Id" package name.

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)

Creating an application

-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]

Display confirmation

--Check the display on your browser. You should see "Hello World!". http://localhost:8080/sample

At the end

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!

reference

I tried to display "Hello World" with VS Code for Spring Boot https://tech-lab.sios.jp/archives/19941

Recommended Posts

Introduction to Java development environment & Spring Boot application created with VS Code
How to build Java development environment with VS Code
Java web application development environment construction with VS Code (struts2)
Prepare Java development environment with VS Code
Create Spring Boot environment with Windows + VS Code
Build Java development environment with VS Code on Mac
Build Java development environment with WSL2 Docker VS Code
[Environment construction] Build a Java development environment with VS Code!
From creating a Spring Boot project to running an application with VS Code
Java Spring environment in vs Code
Spring Boot programming with VS Code
Java development environment (Mac, VS Code)
Build WebAPP development environment with Java + Spring with Visual Studio Code
What I was addicted to when developing a Spring Boot application with VS Code
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Spring Boot + Docker Java development environment construction
Java application development environment created in VM environment
Spring Boot2 Web application development with Visual Studio Code Hello World creation
[Java] Article to add validation with Spring Boot 2.3.1.
[Introduction to Spring Boot] Authentication function with Spring Security
Create a Spring Boot development environment with docker
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
How to boot by environment with Spring Boot of Maven
Build Java program development environment with Visual Studio Code
Beginners create Spring Tools Suite environment with VS Code
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Introduction to Spring Boot Part 1
JavaFX application development with IntelliJ IDEA and Gradle ~ From environment construction to sample code ~
A story that stumbled when deploying a web application created with Spring Boot to EC2
I tried to create a java8 development environment with Chocolatey
[Java] Deploy the Spring Boot application to Azure App Service
Deploy the application created by Spring Boot to Heroku (public) ②
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Deploy the application created by Spring Boot to Heroku (public) ①
A memo to start Java programming with VS Code (2020-04 version)
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
Spring Boot application development in Eclipse
Spring Boot application code review points
Hot deploy with Spring Boot development
Prepare Java development environment with Atom
Inquiry application creation with Spring Boot
Java build with mac vs code
[Java] LINE integration with Spring Boot
Introduction to Spring Boot x OpenAPI ~ OpenAPI made with Generation gap pattern ~
[Java] [Spring] [JavaScript] [gulp] [webpack] [uglify] Private AngularJS application development environment
Let me do VS Code Remote Development + Java development in Proxy environment
Sample code to unit test a Spring Boot controller with MockMvc
Processing at application startup with Spring Boot
Create Spring Boot development environment on Vagrant
Lightweight PHP 7.4 development environment created with Docker
Introduction to algorithms with java --Search (breadth-first search)
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 1/3 [Preparation]
A record of setting up a Java development environment with Visual Studio Code
[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 2
[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 1
Spring5 MVC web application development with Visual Studio Code SQL Server connection
[Code Pipeline x Elastic Beanstalk] CI / CD Java application to Elastic Beanstalk with Code Pipeline Part 3