YouTube Video
Prerequisites
Before starting, ensure you have the following installed:
- Java Development Kit (JDK 11 or later)
Verify installation by running the following command in the terminal:java -version - Visual Studio Code
Download and install VS Code from https://code.visualstudio.com.
Step 1: Install Required Extensions in VS Code
- Open VS Code and click on the Extensions icon in the left sidebar or press
Ctrl+Shift+X. - 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 Spring Boot Extension Pack
Step 2: Create a New Spring Boot Project
Option 1: Using Spring Initializr in VS Code
- Press
Ctrl+Shift+Pto open the Command Palette. - Type and select Spring Initializr: Generate a Maven Project.
- Follow the prompts to configure your project:
- Group ID:
com.example - Artifact ID:
springboot-rest-api - Dependencies: Select
Spring Web
- Group ID:
- Choose a folder to save the project and click Finish.
Step 3: Open the Project in VS Code
- Launch VS Code and click File > Open Folder.
- Navigate to and select your Spring Boot project folder.
- VS Code will recognize it as a Maven or Gradle project and download dependencies automatically.
Step 4: Write Your First Spring Boot REST API
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!"; } }Open the
SpringbootRestApiApplication.javafile and ensure it contains the@SpringBootApplicationannotation: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
- In VS Code, open the Run and Debug view by clicking the bug icon in the sidebar or pressing
Ctrl+Shift+D. - Click Run Java in the Run and Debug panel to start the application.
- Alternatively, run the following command in the integrated terminal:
mvn spring-boot:run - The application will start on port
8080by default.
Step 6: Test the REST API
- Open your browser or use a tool like Postman or cURL to test the endpoint:
http://localhost:8080/ - You should see the response:
Hello, Spring Boot!
Step 7: Add Another REST API Endpoint (Optional)
To add more functionality, create another endpoint:
Modify
HelloController.java:@GetMapping("/greet") public String greet() { return "Welcome to Spring Boot with Visual Studio Code!"; }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 insrc/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!
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner


Comments
Post a Comment
Leave Comment