VS Code – Setting it up as your Java Spring Boot Development Environment
220515
Intro
VS Code is an awesome IDE from Microsoft, recognized as one of the best dev tools by developers’ communities worldwide. According to the Stackoverflow Developer Survey 2022, it remains the most preferred IDE among all developers professionals as well as those who learn to code.
VS Code also ranks at the 1st position according to the GitHub Top IDE Index:
It is available for the 3 main platforms (Linux, macOS, and Windows), it offers code autocomplete (IntelliSense), debugging from within IDE, Built-in Git support, as well as dozens of extensions, themes, linters, and other tools, and to choose from, and finally, also important, it is free!
One of the remarkable things, that I love, is that it can be set up to work with all the major programming languages and frameworks, making it an ideal option for either back-end or front-end development.
If you haven’ yet installed it in your system, go and grab it at the official VS Code site:
If you neither have installed Java nor the VS Code, then you might consider downloading and installing the Coding Pack for Java which includes VS Code, the Java Development Kit (JDK), and essential Java extensions. The Coding Pack can be used as a clean installation, or to update or repair an existing development environment.
Spring Boot, on the other hand, is also a flag-ship framework for Java, that has been widely acknowledged by all developers as a heavy-duty tool for Java development projects.
Some years before, the Spring Boot development environment was dominated by other IDEs from well-known and respectful players, such as Eclipse and IntelliJ IDEA (JetBrains), and especially from Spring Tool Suite – STS (based also in Eclipse). However, from STS version 4, it started distributing as an add-on (extension) for other IDEs: Eclipse, Theia, and VS Code.
In this post, we will take the steps to see how we can set up our Spring Boot development environment, on VS Code.
Note that it is supposed that you have already installed VS Studio on your machine and you have gained enough familiarity with it. It’s worth visiting the VS Code “Setting up VS Code for Java development” to obtain all the necessary info.
Java extensions
Before to proceed, be sure that you have already installed the vital Java extensions in your VS Code IDE. Of course ll the VS Code extensions are available in the VS Code Market-Place.
So, if you haven’t done it yet, all you have to do is just to install the Language Support for Java, as well as the Extension Pack for Java:
Language Support for Java™ by Red Hat
The latest is a collection of popular extensions that can help write, test and debug Java applications in Visual Studio Code. By installing it, the following extensions are installed:
Code Navigation Auto Completion Refactoring Code Snippets
Debugging
Run & Debug JUnit/TestNG Test Cases
This is “must” for your Maven-based projects.
As you can see it t provides a project explorer and shortcuts to execute Maven commands, improving user experience for Java developers who use Maven.
- Support to generate projects from Maven Archetype.
- Support to generate effective POM.
- Provide shortcuts to common goals, plugin goals and customized commands.
- Preserve command history to fast re-run.
- Project Scaffolding
- Custom Goals
Manage Java projects, referenced libraries, resource files, packages, classes, and class members
This extension provides AI-assisted development features for Java developers (as well as for a number of other languages) in Visual Studio Code, with insights based on understanding your code context combined with machine learning.
After that we will proceed to install the Spring Tools 4 for Visual Studio Code.
Spring Tools 4 for Visual Studio Code extensions
Actually, what you have to do is just to go to Spring Boot Extension Pack page in the VS Code Market-Place and install it. You can do that either by using your browser:
Or via the VS Code IDE itself:
This is again an extension pack, which actually consists of 3 separate extensions:
The Spring Boot (Spring Boot Tools)
It is the VSCode extension and Language Server providing support for working with Spring Boot application.properties, application.yml and .java files.
This is the well-known tool to quickly generate a Spring Boot project within the VS Code IDE. It helps you to customize your projects with configurations and manage Spring Boot dependencies.
and
It provides an explorer in the sidebar, and via it, you can view and manage all available Spring Boot projects in your workspace. It also supports features to quickly start, stop or debug a Spring Boot project.
Other Java-related extensions
Tomcat for Java
This extension requires that the Tomcat App server should have been previously installed in your machine.
So, we can add a Tomcat server by choosing the folder that it has been installed.
After that, we can start using it.
Extension features:
- Add Tomcat Server from Tomcat Install Path
- Start/Restart Tomcat Server from VSCode
- Run war package on Tomcat Server
- Debug war package on Tomcat Server
- Run exploded war on Tomcat Server
- Debug exploded war on Tomcat Server
- Open server homepage in browser to check all deployed war packages
- View all deployed war packages in Tomcat Explorer
- Open war package homepage in browser
- Stop Tomcat Server
- Rename Tomcat Server
- Customize JVM Options when starting Tomcat Server
- Reveal deployed war packages in file explorer
- Delete deployed war package
Other useful extensions
There is a really a huge number of extensions available via CS Code Market Place. Below, just a couple of them are mentioned.
XML Tools
Features:
- XML Formatting
- XML Tree View
- XPath Evaluation
- XQuery Linting
- XQuery Execution
- XQuery Code Completion
Spring Boot Snippets
It adds some snippets for Spring Boot. These are especially useful for creating REST apis, and configuring database connection properties.
Java Snippets
spring-crudctrl Spring controller with CRUD actions implemented
spring-crudrepo Spring Data JPA CRUD repository with REST api annotation
spring-jparepo Spring Data JPA repository with REST api annotation
spring-entity JPA entity with ID field
spring-lombok-entity JPA entity with ID field and Lombok @Data annotation
spring-ctrl-getall Controller GET action for collection
spring-ctrl-getone Controller GET action for single item
spring-ctrl-post Controller POST action
spring-ctrl-put Controller PUT action
spring-ctrl-delete Controller DELETE action
application.properties / application.yaml Snippets
spring-sqlite SQLite database jdbc url and properties
spring-sqlserver SQL Server database jdbc url and properties
spring-oracle Oracle database jdbc url and properties
spring-postgresql PostgreSQL database jdbc url and properties
spring-mysql MySQL database jdbc url and properties
spring-h2 H2 database jdbc url and properties
Oracle Developer Tools for VS Code (SQL and PLSQL)
Features include:
- Connect to Oracle Database and Oracle Autonomous Database using Easy Connect syntax, TNS connect aliases, or ODP.NET connection strings
- Create and manage Oracle Autonomous Databases (ADBs) using Oracle Cloud Infrastructure Explorer tree control. Create, start, stop and terminate ADB instances. Automatically download credentials files and create a database connection.
- Oracle Database Explorer tree control: connect and explore Oracle schemas; show data for tables and views and save as CSV or JSON files; auto-generate CREATE, SELECT, INSERT, and DELETE statements for tables; view, edit, debug and save PL/SQL packages, procedures and functions. Run stored procedures and functions with a UI for entering parameter values.
- Edit SQL and PL/SQL with support for hovering, Go to/Peek Definition, Go to/Peek Type Definition and Go to/Peek Implementation. The editor also features IntelliSense for autocompletion of schema object names, procedure/function parameters, and SQL*Plus commands. Navigate through large scripts using breadcrumbs.
- Debug PL/SQL in stored procedures, functions, and packages using Visual Studio Code’s native debugging features. Compile PL/SQL for debugging, step into PL/SQL, and run to a breakpoint. Enable External Application Debugging to listen for and debug PL/SQL procedures and functions called by applications or SQL scripts.
- Execute SQL and PL/SQL and view and save results. View errors in the Problems panel and navigate to the line with the error. Enter bind and substitution variables into a dialog.
- Limited SQL*Plus support
- View SQL command history and view and save SQL bookmarks
Finally, below is a series of pictures that are self-explanatory on how to use Spring Boot Initializr to set up a Maven / pom project.
Using Spring Boot Initializr to set up a new project
- Press
Ctrl (or Alt/Option on Mac) + Shift + P
to open the command palette. - Type
Spring Initializr
to start generating the project.
Basic Project settings
Choose a Maven Project and follow the wizard.
Specify your desired version of Spring Boot
Set the Group id (e.g. your company’s or organization’s domain in reverse order)
Give the project’s Artifact id
Specify the project’s program language
Specify the output package format
Specify your preferable Java version
Select one-by-one the dependencies you plan to work with in your project, e.g.:
Spring Web – This is necessary for developing Web based applications (e.g. MVC), RESTFull services, etc. It also adds an embedded Apache Tomcat server, that it is by default started when we run/debug our project during development
Spring Boot Dev Tools: quite useful even for just thr auto-restart option after changes on our source code
Any other package you think is necessary for your project app, e.g.: JDBC support, Database drivers, etc.
Finally choose your project’s folder and generate it
Below, the generated project pom.xml file is given:
Then you can start coding!
Note: You can continue creating your first REST API project at my post: Quick Intro to Rest API development with Spring Boot.
That’s it!
Enjoy, and thank you for reading? !
Hello there, just became aware of your blog through Google,
and found that it is really informative. I’m going to watch
out for brussels. I will be grateful if you continue this in future.
A lot of people will be benefited from your writing.
Cheers!