Are you ready to build your first Spring Boot application using Spring Tool Suite (STS)? This beginner-friendly tutorial will guide you through creating a simple yet functional Spring Boot web application from scratch. By the end, you'll understand the basics of Spring Boot and have a working application to build upon.
Spring Tool Suite is an Eclipse-based IDE customized for developing Spring applications.
SpringToolSuite4.exe
SpringToolSuite4.app
SpringToolSuite4
When STS opens for the first time, it will ask you to select a workspace directory where your projects will be stored.
In STS, go to File > New > Spring Starter Project
Fill in the project details:
my-first-spring-boot-app
(This will also be used as the artifact ID)com.example
(Use your organization's domain in reverse)Click Next
On the dependencies screen, select:
Click Finish
STS will now create the project and download all required dependencies. This may take a few minutes depending on your internet connection.
Once your project is created, you'll see it in the Package Explorer. Let's explore the key components:
src/main/java: Contains all your Java code
com.example.myfirstapp
: Your base packageMyFirstSpringBootAppApplication.java
: The main application class with the main()
methodsrc/main/resources: Contains non-Java resources
application.properties
: Configuration settings for your applicationstatic/
: Directory for static web resources (HTML, CSS, JavaScript, images)templates/
: Directory for template files (when using template engines)src/test/java: Contains test classes
com.example.myfirstapp
: Package for test classesMyFirstSpringBootAppApplicationTests.java
: Basic test classpom.xml: Maven configuration file that manages dependencies and build settings
Now, let's create a simple REST controller:
Right-click on your base package com.example.myfirstapp
in the Package Explorer
Select New > Class
Name it HelloController
Click Finish
Replace the generated code with the following:
package com.example.myfirstapp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello, %s!", name);
}
}
Let's understand what this code does:
@RestController
: Marks the class as a controller where every method returns a domain object instead of a view@GetMapping("/hello")
: Maps HTTP GET requests to the specified path ("/hello") to this method@RequestParam
: Extracts query parameters from the request URL (e.g., ?name=John
)Now it's time to run your Spring Boot application:
You'll see the console output as Spring Boot starts up. When you see something like Started MyFirstSpringBootAppApplication in x.xxx seconds
, your application is running.
Now that your application is running, let's test it:
http://localhost:8080/hello
You should see:
Hello, World!
Now try passing a name parameter: 3. Navigate to http://localhost:8080/hello?name=Java
You should see:
Hello, Java!
Congratulations! You've just created and tested your first Spring Boot application!
Let's make our application a bit more interesting by returning JSON data instead of plain text:
Greeting
package com.example.myfirstapp;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
HelloController
class:package com.example.myfirstapp;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private static final String TEMPLATE = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/hello")
public Greeting sayHello(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(TEMPLATE, name));
}
}
Save both files and restart your application (right-click project > Run As > Spring Boot App)
Navigate to http://localhost:8080/hello
in your browser
You should now see JSON output:
{"id":1,"content":"Hello, World!"}
Try refreshing the page a few times and notice how the ID increments.
Spring Boot applications can be configured using the application.properties
file:
src/main/resources/application.properties
# Server configuration
server.port=8081
server.servlet.context-path=/api
# Application information
spring.application.name=My First Spring Boot App
Now your application runs on port 8081 instead of the default 8080, and all endpoints have the /api
prefix.
http://localhost:8081/api/hello
Spring Boot is working its magic through several key mechanisms:
Auto-configuration: Spring Boot automatically configures your application based on the dependencies you've added. When you added Spring Web, it configured the necessary components for a web application.
Embedded Server: Spring Boot includes an embedded Tomcat server, eliminating the need to deploy your application to an external server.
Dependency Injection: Spring's dependency injection system manages the components (beans) in your application, creating and providing them as needed.
JSON Conversion: Spring automatically converts your Java objects to JSON when returning them from a @RestController
.
Now that you've built your first Spring Boot application, here are some ways to expand your knowledge:
Application fails to start with "Port already in use" error
application.properties
or stop the other applicationChanges not reflecting after saving files
"Could not resolve dependencies" error
pom.xml
is correctly formattedCongratulations! You've successfully created your first Spring Boot application using Spring Tool Suite. You've learned how to set up a project, create a REST controller, handle request parameters, return JSON responses, and customize application properties.
Spring Boot's convention-over-configuration approach simplifies Java web development, allowing you to focus on your application's business logic rather than boilerplate setup code.
Continue exploring Spring Boot's features and building more complex applications. The skills you've learned here form the foundation for developing robust, production-grade applications with Java and Spring Boot.
Ready to dive deeper into Spring Boot and full stack Java development? Our comprehensive courses cover everything from basic concepts to advanced techniques. Take the next step in your development journey today!
#SpringBoot #JavaDevelopment #SpringTutorial #STS #RESTApi #JavaProgramming #WebDevelopment #BeginnerCoding #SpringToolSuite #BackendDevelopment #JavaFramework #CodeTutorial #SpringMVC #FullStackJava
Leading Full Stack Developer training institute in Bengaluru with 95% placement rate. Industry-aligned Java, SpringBoot, and MERN Stack courses with hands-on projects and expert instructors from top IT companies. Located in BSK 3rd Stage, we serve students from across Karnataka.