Your First Spring Boot Application: Step-by-Step Tutorial in STS

19 Apr, 2025

Your First Spring Boot Application: Step-by-Step Tutorial in STS

Your First Spring Boot Application: Step-by-Step Tutorial in STS

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.

What You'll Learn

  • Setting up Spring Tool Suite (STS)
  • Creating a Spring Boot project
  • Understanding the project structure
  • Building a simple REST API
  • Running and testing your application

Prerequisites

  • Basic Java knowledge
  • JDK 11 or newer installed on your computer
  • Internet connection for downloading STS and dependencies

Step 1: Download and Install Spring Tool Suite (STS)

Spring Tool Suite is an Eclipse-based IDE customized for developing Spring applications.

  1. Visit the official Spring Tool Suite website: https://spring.io/tools
  2. Download the appropriate version for your operating system
  3. Extract the downloaded archive to your preferred location
  4. Launch STS by running the executable file:
    • Windows: SpringToolSuite4.exe
    • macOS: SpringToolSuite4.app
    • Linux: SpringToolSuite4

When STS opens for the first time, it will ask you to select a workspace directory where your projects will be stored.

Step 2: Create a New Spring Boot Project

  1. In STS, go to File > New > Spring Starter Project

  2. Fill in the project details:

    • Name: my-first-spring-boot-app (This will also be used as the artifact ID)
    • Type: Maven
    • Packaging: Jar
    • Java Version: 11 (or your installed version)
    • Language: Java
    • Group: com.example (Use your organization's domain in reverse)
    • Version: 0.0.1-SNAPSHOT (Default is fine)
    • Description: My first Spring Boot application
    • Package: com.example.myfirstapp
  3. Click Next

  4. On the dependencies screen, select:

    • Spring Web (Found under "Web" category)
    • Spring Boot DevTools (Found under "Developer Tools" category)
  5. Click Finish

STS will now create the project and download all required dependencies. This may take a few minutes depending on your internet connection.

Step 3: Understand the Project Structure

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 package
    • MyFirstSpringBootAppApplication.java: The main application class with the main() method
  • src/main/resources: Contains non-Java resources

    • application.properties: Configuration settings for your application
    • static/: 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 classes
    • MyFirstSpringBootAppApplicationTests.java: Basic test class
  • pom.xml: Maven configuration file that manages dependencies and build settings

Step 4: Create Your First Controller

Now, let's create a simple REST controller:

  1. Right-click on your base package com.example.myfirstapp in the Package Explorer

  2. Select New > Class

  3. Name it HelloController

  4. Click Finish

  5. 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)

Step 5: Run Your Application

Now it's time to run your Spring Boot application:

  1. Right-click on your project in the Package Explorer
  2. Select Run As > Spring Boot App

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.

Step 6: Test Your Application

Now that your application is running, let's test it:

  1. Open a web browser
  2. Navigate to 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!

Step 7: Add a Model Class

Let's make our application a bit more interesting by returning JSON data instead of plain text:

  1. Right-click on your base package and create a new class named Greeting
  2. Add the following code:
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;
    }
}
  1. Now, modify your 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));
    }
}
  1. Save both files and restart your application (right-click project > Run As > Spring Boot App)

  2. 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.

Step 8: Customize Application Properties

Spring Boot applications can be configured using the application.properties file:

  1. Open src/main/resources/application.properties
  2. Add the following lines:
# Server configuration
server.port=8081
server.servlet.context-path=/api

# Application information
spring.application.name=My First Spring Boot App
  1. Save the file and restart your application

Now your application runs on port 8081 instead of the default 8080, and all endpoints have the /api prefix.

  1. Test your application by navigating to http://localhost:8081/api/hello

Understanding What's Happening

Spring Boot is working its magic through several key mechanisms:

  1. 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.

  2. Embedded Server: Spring Boot includes an embedded Tomcat server, eliminating the need to deploy your application to an external server.

  3. Dependency Injection: Spring's dependency injection system manages the components (beans) in your application, creating and providing them as needed.

  4. JSON Conversion: Spring automatically converts your Java objects to JSON when returning them from a @RestController.

Going Further: What's Next?

Now that you've built your first Spring Boot application, here are some ways to expand your knowledge:

  1. Connect to a database using Spring Data JPA
  2. Implement CRUD operations (Create, Read, Update, Delete)
  3. Add validation to your input data
  4. Create a complete web interface using Thymeleaf or integrate with a frontend framework
  5. Implement proper error handling for your REST API
  6. Add authentication and authorization using Spring Security

Common Issues and Solutions

Application fails to start with "Port already in use" error

  • Another application is using the specified port
  • Change the port in application.properties or stop the other application

Changes not reflecting after saving files

  • Even with DevTools, some changes require a manual restart
  • Right-click the project and select Run As > Spring Boot App again

"Could not resolve dependencies" error

  • Check your internet connection
  • Verify that your pom.xml is correctly formatted
  • Try right-clicking the project and selecting Maven > Update Project

Conclusion

Congratulations! 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

 

Full Stack Developer Course Inquiries & IT Career Guidance

Contact us: +91 80505 33513

Corporate Java Training & IT Talent Solutions in Bengaluru

Contact us: +91 95359 50350
Octave Gateway, #46, 2nd Floor, 4th Cross Rd, 1st Block Kathriguppe water Tank Road, BSK 3rd Stage Division of TSS, near Acharya Arcade
Bengaluru, Karnataka 560085

Why Choose Techxyte

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.

Techxyte Full Stack Developer Training Logo © 2024 TechXyte. Premier Full Stack Java Developer Training Institute in Bengaluru.