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+P
to 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.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
- 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
8080
by 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!
Comments
Post a Comment
Leave Comment