How to Create a Spring Boot Project in Visual Studio Code

Spring Boot is a powerful framework that simplifies building Java applications. Visual Studio Code (VS Code), combined with its robust extensions, is a lightweight yet powerful IDE for Java development. In this guide, you’ll learn how to set up a Spring Boot project in Visual Studio Code (VS Code) IDE and create and test a simple REST API.

YouTube Video

Prerequisites

Before starting, ensure you have the following installed:

  1. Java Development Kit (JDK 11 or later)
    Verify installation by running the following command in the terminal:
    java -version
    
  2. Visual Studio Code
    Download and install VS Code from https://code.visualstudio.com.

Step 1: Install Required Extensions in VS Code

  1. Open VS Code and click on the Extensions icon in the left sidebar or press Ctrl+Shift+X.
  2. Search for and install the following extensions:
    • Extension Pack for Java: Provides support for Java development.
    • Spring Boot Extension Pack: Includes tools for Spring Boot projects.

Install the Extension Pack for Java

Install the Extension Pack for Java

Install the Spring Boot Extension Pack

Install the Spring Boot Extension Pack

Step 2: Create a New Spring Boot Project

Option 1: Using Spring Initializr in VS Code

  1. Press Ctrl+Shift+P to open the Command Palette.
  2. Type and select Spring Initializr: Generate a Maven Project.
  3. Follow the prompts to configure your project:
    • Group ID: com.example
    • Artifact ID: springboot-rest-api
    • Dependencies: Select Spring Web
  4. Choose a folder to save the project and click Finish.

Step 3: Open the Project in VS Code

  1. Launch VS Code and click File > Open Folder.
  2. Navigate to and select your Spring Boot project folder.
  3. VS Code will recognize it as a Maven or Gradle project and download dependencies automatically.

Step 4: Write Your First Spring Boot REST API

  1. In the src/main/java/com/example/springbootrestapi directory, create a new class named HelloController.java:

    package com.example.springbootrestapi;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @GetMapping("/")
        public String hello() {
            return "Hello, Spring Boot!";
        }
    }
    
  2. Open the SpringbootRestApiApplication.java file and ensure it contains the @SpringBootApplication annotation:

    package com.example.springbootrestapi;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class SpringbootRestApiApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootRestApiApplication.class, args);
        }
    }

Step 5: Run the Spring Boot Application

  1. In VS Code, open the Run and Debug view by clicking the bug icon in the sidebar or pressing Ctrl+Shift+D.
  2. Click Run Java in the Run and Debug panel to start the application.
  3. Alternatively, run the following command in the integrated terminal:
    mvn spring-boot:run
    
  4. The application will start on port 8080 by default.

Step 6: Test the REST API

  1. Open your browser or use a tool like Postman or cURL to test the endpoint:
    http://localhost:8080/
    
  2. You should see the response:
    Hello, Spring Boot!

Step 7: Add Another REST API Endpoint (Optional)

To add more functionality, create another endpoint:

  1. Modify HelloController.java:

    @GetMapping("/greet")
    public String greet() {
        return "Welcome to Spring Boot with Visual Studio Code!";
    }
    
  2. Restart the application and test the new endpoint:

    http://localhost:8080/greet
    

Troubleshooting Tips

  • Dependencies Not Downloading: Ensure Maven or Gradle is installed and properly configured in your system PATH.
  • Port Already in Use: Change the default port by adding the following in application.properties (located in src/main/resources):
    server.port=8081
    
  • Slow IntelliSense: Disable unnecessary extensions to improve performance.

Conclusion

Congratulations! You’ve successfully created a Spring Boot project in Visual Studio Code and built a basic REST API. VS Code, with its powerful extensions, is a fantastic tool for developing Spring Boot applications. Keep exploring the Spring Boot framework and VS Code’s features to enhance your development workflow. Happy coding!

Comments