Visual Studio Code is an ideal lightweight development environment for Spring Boot application developers and there are several useful VS Code extensions including:
If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.
Prerequisites
- To install, launch VS Code and from the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), search for vscode-spring-initializr. Once you have the extension installed, open the Command Palette ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) ) and type Spring Initializr to start generating a Maven or Gradle project and then follow the wizard.
- A lightweight extension to support Lombok annotations processing in Visual Studio Code. VS Code (version 1.21.0 or later) Lombok added as a dependency in your Java Project (Make sure you're using the latest version to avoid issues!) Add with Maven or Add with Gradle; Install. Open VS Code and press Ctrl + Shift + X to.
The features that Visual Studio Code includes out-of-the-box are just the start. VS Code extensions let you add languages, debuggers, and tools to your installation to support your development workflow. VS Code's rich extensibility model lets extension authors plug directly into the VS Code UI and contribute functionality through the same APIs.
A working Java environment with essential extensions installed is needed, including:
- Java Development Kit (JDK), version 11 or later.
- Apache Maven, version 3.0 or later.
For more details, please refer to Java Tutorial
Note: More information about JDK can be found at supported Java versions.
Create the project
The Spring Initializr extension allows you to search for dependencies and generate new Spring Boot projects.
To install, launch VS Code and from the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)), search for vscode-spring-initializr
.
Once you have the extension installed, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and type Spring Initializr
to start generating a Maven or Gradle project and then follow the wizard.
Edit the project
The Spring Initializr extension allows you to edit dependencies after generating a new Spring Boot project.
Visual Studio Code Lombok Not Working
Navigate to your pom.xml
file and right-click to select Edit starters
. The Command Palette will show the dependencies you already have beginning with a √
. You can search for other dependencies you want to add to your project. Or you can click on the existing dependencies to remove them.
Develop the application
The Spring Boot Tools extension includes rich language support for working with Spring Boot application.properties
, application.yml
, and .java
files.
The extension supports quick navigate through source code, smart code completions, quick access to running apps, live application information, and code templates. Similar code completion and validation features are also available for .properties
and .yml
files.
Run the application
In addition to click F5 to run your application, there's another convenient extension 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.
Next steps
- To deploy your web app, see the Deploy a Java Application to Azure tutorial.
- To containerize a web app and deploy as a Docker container, check out the Working with Docker.
- To learn more about Java Debugging features, see Java Debugging Tutorial.
Overview
@SneakyThrows
can be used to sneakily throw checked exceptions without actually declaring this in your method's throws
clause. This somewhat contentious ability should be used carefully, of course. The code generated by lombok will not ignore, wrap, replace, or otherwise modify the thrown checked exception; it simply fakes out the compiler. On the JVM (class file) level, all exceptions, checked or not, can be thrown regardless of the throws
clause of your methods, which is why this works.
Common use cases for when you want to opt out of the checked exception mechanism center around 2 situations:
- A needlessly strict interface, such as
Runnable
- whatever exception propagates out of yourrun()
method, checked or not, it will be passed to theThread
's unhandled exception handler. Catching a checked exception and wrapping it in some sort ofRuntimeException
is only obscuring the real cause of the issue. - An 'impossible' exception. For example,
new String(someByteArray, 'UTF-8');
declares that it can throw anUnsupportedEncodingException
but according to the JVM specification, UTF-8 must always be available. AnUnsupportedEncodingException
here is about as likely as aClassNotFoundError
when you use a String object, and you don't catch those either!
Being constrained by needlessly strict interfaces is particularly common when using lambda syntax (arg -> action
); however, lambdas cannot be annotated, which means it is not so easy to use @SneakyThrows
in combination with lambdas.
Be aware that it is impossible to catch sneakily thrown checked types directly, as javac will not let you write a catch block for an exception type that no method call in the try body declares as thrown. This problem is not relevant in either of the use cases listed above, so let this serve as a warning that you should not use the @SneakyThrows
mechanism without some deliberation!
Visual Studio Code Java Lombok
You can pass any number of exceptions to the @SneakyThrows
annotation. If you pass no exceptions, you may throw any exception sneakily.