How to Create a Spring Boot Project in Visual Studio Code (VS Code)

In this tutorial, we will walk through the process of creating a Spring Boot project using Visual Studio Code (VS Code). We will cover setting up VS Code, creating a new Spring Boot project using Spring Initializr, and running your first Spring Boot application.

Prerequisites

Before we start, ensure you have the following:

  • Java Development Kit (JDK) installed
  • Visual Studio Code (VS Code) installed
  • Java Extension Pack for VS Code installed
  • Maven installed (optional, but recommended)

Step 1: Setting Up Visual Studio Code

  1. Download and Install VS Code:

    • Download VS Code from the official website.
    • Follow the installation instructions for your operating system.
  2. Install Java Extension Pack:

    • Open VS Code.
    • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.
    • In the search box, type Java Extension Pack and install it. This pack includes several useful extensions for Java development, including support for Spring Boot.

Step 2: Creating a New Spring Boot Project

  1. Open Spring Initializr:

    • In VS Code, press Ctrl+Shift+P to open the Command Palette.
    • Type Spring Initializr: Generate a Maven Project and select it.
  2. Configure Project Metadata:

    • Group Id: Enter your group ID (e.g., com.example).
    • Artifact Id: Enter your artifact ID (e.g., spring-boot-demo).
    • Version: Enter the version of your project (e.g., 0.0.1-SNAPSHOT).
    • Name: Enter the name of your project (e.g., spring-boot-demo).
    • Description: Enter a description for your project (e.g., Demo project for Spring Boot).
    • Package Name: Enter the package name (e.g., com.example.demo).
    • Packaging: Choose Jar.
    • Java Version: Ensure you select the appropriate Java version (e.g., 17 if you are using JDK 17).
    • Click Next.
  3. Select Dependencies:

    • On the Dependencies screen, select the dependencies you need. For a basic Spring Boot application, you can start with:
      • Spring Web
    • Click Finish.

VS Code will generate a new Spring Boot project with the specified configurations and dependencies. You will be prompted to open the project in a new window.

Step 3: Exploring the Project Structure

After creating the project, you will see the following structure in your VS Code Explorer:

spring-boot-demo
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── demo
│   │   │               └── SpringBootDemoApplication.java
│   │   └── resources
│   │       ├── application.properties
│   └── test
│       └── java
│           └── com
│               └── example
│                   └── demo
│                       └── SpringBootDemoApplicationTests.java
└── pom.xml

Key Files and Directories

  • src/main/java: Contains your main application code.
  • src/main/resources: Contains configuration files (e.g., application.properties).
  • src/test/java: Contains your test code.
  • pom.xml: Maven configuration file.

Step 4: Writing a Simple REST Controller

  1. Create a Controller:
    • In the src/main/java/com/example/demo directory, create a new package named controller.
    • In the controller package, create a new Java class named HelloController.

HelloController.java

package com.example.demo.controller;

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

@RestController
@RequestMapping("/api")
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Step 5: Running the Application

  1. Open the Main Class:

    • Open the SpringBootDemoApplication class in the src/main/java/com/example/demo directory.
  2. Run the Application:

    • Press F5 to run the application. Alternatively, you can open the integrated terminal (`Ctrl+``) and run the application using Maven:
      ./mvnw spring-boot:run
      

VS Code will start the Spring Boot application, and you should see output in the terminal indicating that the application has started successfully.

Step 6: Testing the Application

  1. Open a Web Browser:
    • Open your web browser and navigate to http://localhost:8080/api/hello.

You should see the response Hello, World! from your Spring Boot application.

Conclusion

In this tutorial, we have walked through the process of creating a Spring Boot project in Visual Studio Code (VS Code). We used Spring Initializr to generate a new Spring Boot project, added a simple REST controller, and ran the application. By following these steps, you should now have a basic Spring Boot REST API application running in VS Code. This setup provides a solid foundation for developing more complex Spring Boot applications.


Comments