Run Spring Boot Maven Project in VS Code (Visual Studio Code)

Visual Studio Code (VS Code) is a lightweight but powerful source code editor that supports various development tasks, including running Spring Boot applications. In this tutorial, we will walk you through the steps to create, import, and run a Spring Boot Maven project in VS Code.

Prerequisites

  • JDK 17 or later
  • Maven
  • VS Code installed on your machine
  • Spring Boot (version 3.2+ recommended)
  • Java Extension Pack installed in VS Code

Step 1: Set Up a Spring Boot Project Using Spring Initializr

Use Spring Initializr to generate a new Spring Boot project with the following configuration:

  • Project: Maven Project
  • Language: Java
  • Spring Boot: 3.2.x
  • Dependencies: Spring Web

Download the generated project, unzip it, and open it in VS Code.

Example Project Structure

The basic structure of a Spring Boot project with Maven looks like this:

my-spring-boot-app/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/demo/
│   │   │       └── DemoApplication.java
│   │   └── resources/
│   │       ├── application.properties
│   └── test/
│       └── java/
│           └── com/example/demo/
│               └── DemoApplicationTests.java
├── mvnw
├── mvnw.cmd
├── pom.xml
└── .mvn/
    └── wrapper/
        └── maven-wrapper.properties

Step 2: Open the Project in VS Code

  1. Open VS Code: Launch VS Code on your machine.

  2. Open Project:

    • Click on File -> Open Folder....
    • Navigate to the root directory of your Spring Boot project and click Select Folder.

VS Code will open the project and load the necessary files.

Step 3: Install Java Extensions

Ensure you have the Java Extension Pack installed in VS Code. If not, you can install it from the Extensions view:

  1. Open Extensions View:

    • Click on the Extensions icon in the Activity Bar on the side of the window or press Ctrl+Shift+X.
  2. Search for Java Extension Pack:

    • Type Java Extension Pack in the search bar and press Enter.
  3. Install the Extension Pack:

    • Click the Install button next to the Java Extension Pack.

This will install essential Java extensions, including support for Maven and Spring Boot.

Step 4: Create the Application Class

Create a Java class named DemoApplication in the src/main/java/com/example/demo directory.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Explanation:

  • @SpringBootApplication: Marks this class as the main entry point for the Spring Boot application.
  • main method: Starts the Spring Boot application.

Step 5: Create a Simple REST Controller

To verify the application works as expected, let's create a simple REST controller.

Create a Java class named HelloController in the src/main/java/com/example/demo directory:

package com.example.demo;

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

@RestController
public class HelloController {

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

Explanation:

  • @RestController: Marks this class as a REST controller.
  • @GetMapping("/hello"): Maps HTTP GET requests to the /hello endpoint to the sayHello method.
  • sayHello method: Returns a "Hello, World!" message.

Step 6: Run the Application

Using the Integrated Terminal

  1. Open Terminal:

    • Click on Terminal -> New Terminal or press Ctrl+ (backtick).
  2. Run the Application:

    • In the terminal, navigate to the root directory of your project (if not already there).
    • Use the Maven wrapper to run the Spring Boot application:

For Unix/Linux/macOS:

./mvnw spring-boot:run

For Windows:

mvnw.cmd spring-boot:run

Using the VS Code Debugger

  1. Add Debug Configuration:

    • Click on the Run icon in the Activity Bar on the side of the window or press Ctrl+Shift+D.
    • Click on create a launch.json file and select Java environment.
    • VS Code will create a launch.json file with default configurations.
  2. Modify launch.json:

    • Ensure that the launch.json configuration looks like this:
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "Spring Boot-DemoApplication<demo>",
      "request": "launch",
      "mainClass": "com.example.demo.DemoApplication",
      "projectName": "demo"
    }
  ]
}
  1. Run the Application:
    • Click the green play button next to the configuration name in the Run view or press F5.

Step 7: Verify the Application

Open a web browser or a tool like Postman and navigate to the following URL to verify the application:

  • Hello Endpoint:
    • URL: http://localhost:8080/hello
    • Method: GET
    • Response: Hello, World!

You should see the "Hello, World!" message returned by the HelloController.

Step 8: Creating a Test Class

Create a Java class named DemoApplicationTests in the src/test/java/com/example/demo directory.

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }
}

Explanation:

  • @SpringBootTest: Indicates that this is a Spring Boot test.
  • contextLoads method: Tests if the Spring application context loads successfully.

Running Tests

  1. Open the Test File:

    • Navigate to the DemoApplicationTests class in the Explorer view.
  2. Run Tests:

    • Right-click on the DemoApplicationTests class and select Run Tests.
  3. View Test Results:

    • The Test Explorer will show the test results, indicating whether the tests passed successfully.

Conclusion

In this tutorial, you have learned how to create, import, and run a Spring Boot Maven project in VS Code. We covered:

  • Setting up a Spring Boot project using Spring Initializr with Maven.
  • Importing the Maven project into VS Code.
  • Installing necessary Java extensions in VS Code.
  • Creating the main application class.
  • Creating a simple REST controller to verify the application works as expected.
  • Running the application using the integrated terminal and the VS Code debugger.
  • Creating and running tests.

By following these steps, you can easily set up and manage Spring Boot projects using Maven in VS Code, leveraging its powerful development and debugging features.

Comments