This article is a step by step guide to setup and develop Spring Boot applications using Visual Studio Code. I will be building a simple Spring Boot service from scratch using VS Code. I’ll show you what extensions to install and how to use the VS Code features to develop and iterate on your Spring Boot application.

How good Visual Studio Code is

As Java developers we all love IntelliJ Idea, this is the most popular editor tool for Java development, the second one is Eclipse, and then Netbeans. Today, i want to do is try a completely different program for Java development and see if it works and the program i am talking about is Visual Studio Code. How good is Visual Studio Code for working on Java projects? Does it works? Can we get work done with this thing? And i am gonna put this to the test by writing a simple Hello world job application. I am going to build a Spring Boot service and by the end of the coding this program we will see how good Visual Studio Code is and whether we can do serious Java development with it.

So one of the reasons why java developer love IDE because none of us remember how to compile java code using the command line. You can not be bothered to remember all the compiler flags and all that IDE make it very simple, you just click and you can build projects, you have auto complete, all that good stuff and we have come to rely on it as java developers. So when you say okay i want to not use a full-fledged IDE and i want to use something like Visual Studio Code which is actually not a full IDE. The question comes is it even possible to work on this thing because Visual Studio Code is kind of like a very popular choice for front-end developers not so much for back-end developers. So the question is how good is java development experience with VS Code? Now you might wonder why am doing this why would you even bother with VS Code, why not just to use IntelliJ or Eclipse. The thing is well some of us are full stack developers we like to work on both the front end and back end so when you’re working on front end you’re very likely working on Javascript technologies and you are using Visual Studio Code and Javascripty and Typescript for it. So rather than having to switch between two editors right, front end with VS Code, back end with IntelliJ, why not try Java with VS Code and see how it works.

Install required extensions

So i am going to show you a clean install of VS Code, i don’t have anything installed apart from like a theme that i’ve customized. And now what’s i am going to do is to add the required extensions

The most popular one is Extension Pack for Java:

VS Code has a concept of extension packs which is basically a bunch of different extensions bundled together and you click that one install button it’s going to install all those extensions.

The second one is Spring Boot Extension Pack:

Create Spring Boot project

Now i am going to create a new Java project. You notice here now there is a Create Java Project button after you install all above extensions, this wasn’t there before but now it’s come up.

And instead of creating a new Java project, i want to create a new Spring Boot project. And this is where the first one of extensions that we’ve installed comes into the picture.

How would you create a new Spring Boot project, you go to the Spring Initializr and you configure this. But here with the Spring Boot Extension Pack installed in VS Code, you can actually do that in VS Code and you don’t have to go to the website.

The first thing you open the Command Palette by typing in Windows is Ctrl + Shift + P and in Mac is Cmd + Shift + P . Then in the text box, you type “Spring” -> choose Spring Initialzr: Create a Maven Project… -> hit Enter, because this is basically the equivalent of going to start.spring.io website.

The next step is choosing Spring Boot stable version is 2.7.15

And then you need to specify the project language is Java

And then you need to input the Group ID

And then you need to input the Artifact ID

And then you need to provide the packaging type is JAR

And then you need to specify the Java version is 11

And now you need to choose the dependencies that you need for the Spring Boot application. I am going to choose a bunch of these stuff :

  • Spring Boot DevTools
  • Spring Web

Finally we press Enter to continue and now it’s telling me where we want to save this project. Then it will going to generate Spring Boot app by downloading it from start.spring.io and then it’s going to put it there.

So now it has given me this prompt over bottom right corner which indicates that successfully generated location in the code directory.

You can click on the Open button and it is going to open this project:

So here you can see the Maven wrapper for Windows (mvnw.cmd) and Linux (mvnw). Maven wrapper is basically the script file that is kind of wraps maven so you don’t have to install it. Then maven wrapper is the command and this is going to take the place of an installed maven.

So here you can see the Spring Boot project with folder structure and java project structure. So now i have the project setup and ready to go. This is actually a step up from the IntelliJ free edition, because creating a new spring project is not possible in the community edition, means you need paid version of IntelliJ version to create spring projects.

Run Spring Boot application

Open the class IsTheSiteUpApplication in the package com.favtuts.isthesiteup, and click on the Run link to run the main method. It detects a java main method so it know how to run it.

Here you can see my Spring Boot application is up and running under embedded Tomcat server on the port 8080. You can verify by open the browser and make a request (http://localhost:8080/) to make sure you get the error page:

So you don’t need to remember the Java compiler flags even when you’re working on Java in VS Code.

Writing the real code

I am going to create an app which takes in an URL and it checks by making a ping to make sure that URL is up. So the first thing i need is a controller by right click on the package com.favtuts.isthesiteup and choose New -> Java class

It will promt you input the text as format: package_name.class_name (here is controllers.UrlCheckController)

After press Enter, then you can see the new Java class is generated in the package controllers.

Next, i will make this a REST controller because i will be making a REST API for this they can pass in a query parameter which is the URL to check, and it’s going to tell this site is up or down.

package com.favtuts.isthesiteup.controllers;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UrlCheckController {

    private final String SITE_IS_UP = "Site is up!";
    private final String SITE_IS_DOWN = "Site is down!";
    private final String INCORRECT_URL = "URL is incorect!";

    @GetMapping("/check")
    public String getUrlStatusMessage(@RequestParam String url) {
        String returnMessage = ""        ;
        try {
            URL urlObj = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            int responseCodeCategory = conn.getResponseCode() / 100;
            boolean isDownCheck = (responseCodeCategory != 2) && (responseCodeCategory != 3);
            if (isDownCheck) {
                returnMessage = SITE_IS_DOWN;
            } else {
                returnMessage = SITE_IS_UP;
            }
        } catch (MalformedURLException e) {
            returnMessage = INCORRECT_URL;
        } catch (IOException e) {
            returnMessage = SITE_IS_DOWN;
        }

        return returnMessage;
    }
    
}

Test the Spring Boot application

Check the URL = https://www.tuts.heomi.net

Check the not found URL = https://github.com/favtuts/java-spring-boot-tutorials-123

Source Code on Github

Leave a Reply

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