Spring Boot Hello World Example Using STS
In this tutorial, you will learn how to build a Spring Boot Hello World application using Spring Tool Suite (STS). You will download STS, create a Spring Starter Project with the Spring Web dependency, add a @RestController, and run your first REST endpoint.
Download and Install STS
First, download STS from the official Spring website:
Download Spring Tool Suite (STS) from spring.io/tools
Download and install it on your machine, then launch STS.
Choose a Workspace
Select or create a workspace directory where your projects will be stored.
Create a Spring Starter Project
From the File menu, choose New → Spring Starter Project.
Configure Project Metadata
Enter your project details such as name, artifact-id, group-id, type, packaging, and Java version.
Add Spring Web Dependency
On the dependencies screen, select Spring Web. This brings in Spring MVC and an embedded Tomcat server.
When you click Finish, make sure you have a stable internet connection because Maven downloads dependencies from the remote repository into your local repository.
Generated Project Structure
STS will generate the default project structure and code.
Open FirstExampleApplication.java. It will look like this:
package com.deepsingh44.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstExampleApplication {
public static void main(String[] args) {
SpringApplication.run(FirstExampleApplication.class, args);
}
}
Create HelloController
Create a new class named HelloController.java inside the same package:
package com.deepsingh44.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/")
public String helloWorld() {
return "Hello First Program";
}
}
Run the Application
Right-click your project and choose Run As → Spring Boot App.
Fix Port 8080 Conflict
If your application shows an error that port 8080 is already in use:
Change the port number in src/main/resources/application.properties:
server.port=8888
See the Result
Run the application again. It will start successfully:
Open any browser and visit:
You should see Hello First Program in your browser.
Frequently Asked Questions
What is Spring Boot?
Spring Boot is an opinionated extension of the Spring Framework that removes boilerplate configuration. It provides auto-configuration, embedded servers, and production-ready features so you can create stand-alone Java applications quickly.
What is STS in Spring Boot?
STS stands for Spring Tool Suite, an Eclipse-based IDE tuned for Spring projects. It includes Spring-aware tooling, project templates, and a built-in Spring Starter wizard.
Why am I getting "port 8080 was already in use" in Spring Boot?
Another process is already using port 8080. You can either stop that process or change the port by adding server.port=8888 in src/main/resources/application.properties.
What is the difference between @Controller and @RestController?
@Controller is used for traditional Spring MVC and returns view names. @RestController is a convenience annotation that combines @Controller and @ResponseBody, so each method returns data directly as JSON or plain text.
Which dependency do I need for a Spring Boot REST API?
Add the Spring Web dependency in Spring Initializr or Spring Starter Project. It brings in Spring MVC and an embedded Tomcat server so you can expose REST endpoints.
Conclusion
In this tutorial, you built your first Spring Boot Hello World application using STS. You downloaded Spring Tool Suite, created a Spring Starter Project with Spring Web, added a @RestController, handled the port 8080 conflict, and confirmed the endpoint in your browser.
Related topics: Spring Boot REST API, Spring Boot Annotations, Spring Web Dependency, Spring Starter Project, Embedded Tomcat, Spring Boot Application Properties
No comments:
Post a Comment