How to setup Visual Studio Code for Spring Boot development

Visual Studio Code  is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on – Linux, macOS, and Windows.

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”. It takes an opinionated view of the Spring platform and third-party libraries so one can get started with minimum fuss. Most Spring Boot applications need minimal Spring configuration.

Eclipse & IntelliJ IDEA are typically suggested for spring boot development.

I use VS Code for everything because it’s fast, lightweight, powerful & provides a rich user experience. Now that VS Code supports Java + Spring Boot development, here is how you can set it up too.

Setup for Java

VS Code works with all major versions of Java SE from 11.

Auto Setup

Visual Studio Code offers special installers for Java developers, which include all the necessary dependencies and extensions for Java development in Visual Studio Code as below.

The installer automatically tries to detect a JDK, VS Code, and essential Java extensions. During install, it downloads the stable versions of those tools from trusted online sources and then installs and configures them.

Manual Setup

Alternatively, you can install JDK, VS Code or extensions separately as per your preference as below.

  1. Download & install a Java SE Development Kit (JDK) from one of these sources
    Oracle Java SE
    AdoptOpenJDK
    Azul Zulu for Azure – Enterprise Edition
  1. Install Visual Studio Code and enable Java support by installing the Microsoft Java Extension Pack extension.
  1. Tell VS Code where the JDK is located by setting the value of the  JAVA_HOME system environment variable to the install location of the JDK. Alternatively, if you want to configure only VS Code to use the JDK, use the  java.home setting in VS Code’s User or Workspace settings ( โŒ˜,).

Steps for setting JAVA_HOME varies for different versions of Windows & MacOS and is outside the scope of this article.

Setup for Spring Boot

To enable VS Code for Spring Boot development, install the following extensions.

Create the Project

Launch VS Code and open the command palette ( โ‡งโŒ˜P) and type Spring Initializr to start generating a Maven or Gradle project and then follow the wizard.

Edit the Project

Create a web controller by adding the following code to the src/main/java/com/example/demo/HelloController.java file.

Run the Project

Click  F5 to run your application or use the Spring Boot Dashboard with which you can view and manage all available Spring Boot projects in your workspace as well as quickly start, stop, or debug your project.

References

Leave a Reply

Your email address will not be published. Required fields are marked *