Develop your Spring Boot application with Visual Studio Code

José Collado, EDICOM Software Engineer, explains how to develop your Spring Boot application with Visual Studio Code. Discover how to create a project, launch the application, reasons and requirements for using VSCode.

    Written by:

    José Collado

    Full Stack Developer

    Full Stack Developer specialized in Angular and Spring Boot, with an interest in User Experience and Usability.

Introduction

Nowadays, the tools involved in the software development process are becoming increasingly important. This is why choosing the appropriate tools can make the developers’ work easier and enable companies to produce better quality software.

What is an IDE and why VSCode?

An integrated development environment or IDE is an application that facilitates the development of new applications. They are designed to cover all programming tasks in one place, providing a central interface with all the tools a developer needs, such as a code editor, compiler, debugger or automation tools.

Since it was launched in 2015, Visual Studio Code (VSCode) has seen enormous growth in the developer community, being chosen by 71% of developers according to StackOverflow.

This success is largely due to the following factors:

  • Multiplatform (macOS, Linux and Windows)
  • Free of charge and open code.
  • Multilanguage.
  • Customizable by means of extensions.
  • Fast and light.

Although the initial purpose of Visual Studio Code was as a code editor, it is the extensions that add support for almost any programming language and transform it into a powerful IDE.

Requirements

To follow this article, it is necessary to have installed on your machine:

In VSCode we will need to install the following extensions:

Project creation

To create the initial structure of the Spring Boot project we will use Spring Initializr, which will generate a Spring base project with all the options we have selected.

We can also use Spring Initializr from VSCode, launching the command palette with Ctrl+Shift+P and executing the Spring Initializr command: Create a Maven Project...

For this guide we will use Maven, Java 17, Spring Boot 2.5.5 and Lombok and Spring Security as dependencies.

Launching the application

We extract the zip file generated with Spring Initializr and open the folder from Visual Studio Code.

Once the project is loaded, we will see that it detects the project as a Spring Boot application.

Before launching the application for the first time, we run the install command from Maven to download the dependencies. We can do it from the Maven plugin integrated with VSCode (left sidebar) or from the terminal.

  • Note: By default, VSCode Maven extension uses the Maven executable (mvnw) included in the project. You can specify the path to your local Maven installation from Settings (maven.executable.path).

Finally, we launch the application from the Spring Boot Dashboard.

Next steps

  • Within the project, we can create a folder called .vscode, where the specific configuration files for the project are located. In addition, if we use version control such as Git, we can include these files to have a common configuration with the rest of the developers in the team.

These are some of the useful files we can add:

  • settings.json: VSCode settings applied at project level. For example, we can configure a common code formatter for the whole team.
  • launch.json: Lets you define the parameters with which the application is launched, such as extra parameters of the JVM or environment variables needed to launch the application.
  • extensions.json: VSCode extensions recommended for our project. If this file exists, when opening the VSCode project, it will recommend installing them if we do not have them.

This would be the extensions.json file with the three extensions required for this guide:

  • Although VSCode detects the Java version of the project and the JDK installed on our computer, we can make adjustments and see the JDKs installed using Control + Shift + P from the option Java: Configure Java Runtime
  • Other useful commands provided by the Java extension:
    • Java: Update Project: Forces project configuration / classpath updates (e.g. changes of dependencies or Java compilation level)
    • Java: Clean Java Language Server Workspace: Clears the Java language server workspace.
    • Java: Import Java Projects into Workspace: Detects and imports all Java projects in the workspace.
    • Developer: Reload Window: Relaunches the VSCode window.

Debugging

Launching the project in Debug mode(F5),we can easily debug our code.

Testing

From the VSCode sidebar we can open the Tests explorer and launch our unit tests one by one, or all the tests of the project.

Conclusion

As we have seen, Visual Studio Code makes our work much easier when creating and developing our Spring Boot projects. Moreover, with a few hours of use we will notice its lightness and speed compared to other IDEs, saving time in configuration and start-up.

Finally, we can continue to take advantage of VSCode’s potential by changing the background colours of the editor or add more functionalities by installing extensions.