codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

3 / 62 topics
1Getting Started with Spring Boot2Installation of Spring Boot3Creating Your First Spring Boot Application4Understanding the Project Structure5Managing Dependencies with Maven/Gradle
Tutorials/Spring Boot/Creating Your First Spring Boot Application
🍃Spring Boot

Creating Your First Spring Boot Application

Updated 2026-04-20
5 min read

Introduction

Spring Boot is a powerful framework for building production-ready applications with minimal configuration. It simplifies the development process by providing default configurations and reducing the amount of boilerplate code you need to write. In this tutorial, we will walk through creating your first Spring Boot application from scratch.

Prerequisites

Before you start, ensure you have the following installed on your machine:

  • Java Development Kit (JDK) 8 or higher
  • Maven or Gradle
  • An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or Visual Studio Code
  • Git (optional but recommended for version control)

Step 1: Setting Up Your Project

Using Spring Initializr

Spring Initializr is a web-based tool that helps you bootstrap your new Spring Boot project. It provides a simple interface to configure your project's dependencies and generates the necessary files.

  1. Access Spring Initializr:

    • Open your browser and go to https://start.spring.io/.
  2. Configure Your Project:

    • Project: Maven Project
    • Language: Java
    • Spring Boot: Select the latest stable version
    • Project Metadata:
      • Group: com.example
      • Artifact: demo
      • Name: demo
      • Description: Demo project for Spring Boot
      • Package name: com.example.demo
    • Packaging: Jar
    • Java: 8 or higher
  3. Dependencies:

    • Add the following dependencies:
      • Spring Web (for building web applications)
      • Spring Data JPA (for database interaction)
      • H2 Database (an in-memory database for testing)
  4. Generate Project:

    • Click on "Generate" to download a ZIP file of your project.
  5. Import the Project into Your IDE:

    • Unzip the downloaded file.
    • Import the project into your IDE. For IntelliJ IDEA, you can use File > New > Project from Existing Sources.

Using Spring CLI (Optional)

If you prefer using the command line, you can install the Spring CLI and generate a project with it.

  1. Install Spring CLI:

    • Follow the instructions on Spring's official website to install the Spring CLI.
  2. Generate Project:

    spring init --dependencies=web,data-jpa,h2 \
      --build=maven \
      --language=java \
      --name=demo \
      --package-name=com.example.demo \
      my-spring-boot-app
    
  3. Import the Project into Your IDE:

    • Navigate to the generated project directory and import it into your IDE.

Step 2: Exploring the Generated Project Structure

After generating your project, you will see a standard Maven project structure. Here are some key files and directories:

  • src/main/java/com/example/demo: Contains your Java source code.
  • src/main/resources: Contains configuration files like application.properties.
  • pom.xml (for Maven) or build.gradle (for Gradle): Project build configuration.

Step 3: Creating a Simple REST Controller

Let's create a simple REST controller to handle HTTP requests.

  1. Create a New Java Class:

    • Navigate to src/main/java/com/example/demo.
    • Create a new package named controller.
    • Inside the controller package, create a new Java class named HelloController.
  2. Implement the Controller:

    package com.example.demo.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/api")
    public class HelloController {
    
        @GetMapping("/hello")
        public String sayHello() {
            return "Hello, Spring Boot!";
        }
    }
    
    • @RestController: This annotation is a combination of @Controller and @ResponseBody, making it easier to create RESTful web services.
    • @RequestMapping("/api"): Maps HTTP requests to handler methods of MVC and REST controllers.
    • @GetMapping("/hello"): Maps HTTP GET requests onto specific handler methods.

Step 4: Running Your Application

Spring Boot applications can be run directly from your IDE or using the command line.

Running from IDE

  1. Run the Main Class:

    • Locate DemoApplication.java in src/main/java/com/example/demo.
    • Right-click on the class and select "Run" (or press the play button).
  2. Access the Application:

    • Open your browser and go to http://localhost:8080/api/hello.
    • You should see the message "Hello, Spring Boot!".

Running from Command Line

  1. Navigate to Project Directory:

    cd path/to/your/project
    
  2. Build and Run the Application:

    mvn spring-boot:run
    
  3. Access the Application:

    • Follow the same steps as above to access the application.

Step 5: Adding a Simple Entity

Let's create a simple entity class to represent data in our application.

  1. Create a New Java Class:

    • Navigate to src/main/java/com/example/demo.
    • Create a new package named model.
    • Inside the model package, create a new Java class named User.
  2. Implement the Entity:

    package com.example.demo.model;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    @Entity
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        private String name;
        private String email;
    
        // Getters and Setters
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    }
    
    • @Entity: Marks the class as a JPA entity.
    • @Id and @GeneratedValue: Define the primary key of the entity.

Step 6: Creating a Repository

Let's create a repository interface to interact with the database.

  1. Create a New Java Interface:

    • Navigate to src/main/java/com/example/demo.
    • Create a new package named repository.
    • Inside the repository package, create a new Java interface named UserRepository.
  2. Implement the Repository:

    package com.example.demo.repository;
    
    import com.example.demo.model.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface UserRepository extends JpaRepository<User, Long> {
        // No need to write any methods; Spring Data JPA provides them automatically.
    }
    
    • JpaRepository: Provides CRUD operations for the entity.

Step 7: Creating a REST Controller for Users

Let's create a new controller to handle user-related HTTP requests.

  1. Create a New Java Class:

    • Navigate to src/main/java/com/example/demo.
    • Create a new package named controller.
    • Inside the controller package, create a new Java class named UserController.
  2. Implement the Controller:

    package com.example.demo.controller;
    
    import com.example.demo.model.User;
    import com.example.demo.repository.UserRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/api/users")
    public class UserController {
    
        @Autowired
        private UserRepository userRepository;
    
        @GetMapping
        public List<User> getAllUsers() {
            return userRepository.findAll();
        }
    
        @PostMapping
        public User createUser(@RequestBody User user) {
            return userRepository.save(user);
        }
    }
    
    • @Autowired: Injects the UserRepository dependency.
    • @GetMapping and @PostMapping: Map HTTP GET and POST requests to handler methods.

Step 8: Testing Your Application

You can test your application using tools like Postman or curl.

  1. Get All Users:

    curl http://localhost:8080/api/users
    
  2. Create a New User:

    curl -X POST http://localhost:8080/api/users \
      -H "Content-Type: application/json" \
      -d '{"name": "John Doe", "email": "john.doe@example.com"}'
    

Conclusion

In this tutorial, we have created a simple Spring Boot application with a REST controller and a database entity. We explored the project structure, set up dependencies, and implemented basic CRUD operations. This should give you a solid foundation to build more complex applications using Spring Boot.

Best Practices

  • Use meaningful names for your packages and classes.
  • Keep your code organized by separating concerns (e.g., controllers, services, repositories).
  • Utilize Spring Data JPA for database interactions to reduce boilerplate code.
  • Configure your application properties in application.properties or application.yml.
  • Use version control systems like Git to manage changes and collaborate with others.

By following these best practices, you can develop robust and maintainable Spring Boot applications.


PreviousInstallation of Spring BootNext Understanding the Project Structure

Recommended Gear

Installation of Spring BootUnderstanding the Project Structure