In this tutorial we show how to create a new spring-boot project in Visual Studio Code using a wizard.
Spring Initializr built-in
The way we would normally create a new Spring boot project is to open the Spring Initializr in a browser, input a few parameters, download the project and import it in our IDE of choice.
With Visual Studio Code the process is much simpler as Spring Initializr’s functionality is wrapped in the extensions that we installed in the first article of this series.
All it took was a few clicks and our new project is ready to build in Maven.
REST endpoint
Now let’s add a simple REST endpoint to our project.
To keep things simple we go for a GET with two text parameters, needle and haystack.
The annotation @GetMapping isn’t contained in the libraries that we imported so it breaks the project immediately. That is to be expected, this annotation is contained in the spring-web artifact so we need to import it in our pom.xml file.
Here we are clearly missing the IntelliJ IDEA functionality to guess which artifact we need to import and find a way to solve the issue for us. Instead, we are forced to: leave the editor, search on the web to which artifact the annotation belongs to and add that artifact manually to our project.
IntelliJ would have solved this for us in one click so it’s quite underwhelming that VS Code doesn’t have a similar functionality.
Now we are able to build our application that contains a simple REST endpoint.
Start-up the spring-boot application
What happens if we try to run our application?
mvn spring-boot:run
The application starts successfully but terminates immediately.
Again, we’re missing an important dependency in our pom.xml file but VS Code doesn’t know and doesn’t help.
The missing dependency is of course the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Once we add the correct dependency and re-run the Maven command, our application is finally up-and-running and ready to receive requests.
In this tutorial we discussed how to create a new spring-boot project in Visual Studio Code using Spring Initializr, we setup a controller class and create a simple REST endpoint. Finally, we build and start-up our application after importing the correct dependencies.