Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

How to Create a Simple Spring Boot Project?

Creating a simple Spring Boot project is made easy through the use of the Spring Initializr web service. Here's a step-by-step guide to setting up a basic Spring Boot project:

1. Use Spring Initializr:

  1. Go to Spring Initializr.
  2. Select your project type: Maven (most common) or Gradle.
  3. Choose the language: Java (most common), Kotlin, or Groovy.
  4. Fill out the project metadata:
    • Group: Your organization's domain (e.g., com.example).
    • Artifact: The name of your project (e.g., myspringbootapp).
    • Name: The display name of your project (can be the same as the Artifact).
    • Description: A short description of your project.
    • Package: Package name for your source files.
    • Packaging: Choose Jar (common for microservices) or War (for traditional Java EE servers).
    • Java version: Usually the latest stable version.
  5. Add dependencies for your project. For a basic web application, add Spring Web under the "Web" section.
  6. Click Generate to download a .zip file containing your new project.

2. Unzip and Open the Project:

  1. Extract the .zip file you downloaded.
  2. Open the project in your preferred Integrated Development Environment (IDE). Most IDEs like IntelliJ IDEA, Eclipse, or Visual Studio Code support Spring Boot projects out of the box or with a plugin.

3. Explore the Project:

The structure of a Spring Boot project created by Spring Initializr is:

  • src/main/java: Contains the main application file with the @SpringBootApplication annotation and any other Java classes you add.
  • src/main/resources: Contains configuration files like application.properties or application.yml and static or template resources.
  • src/test/java: Contains test classes.

4. Run the Application:

If you added the Spring Web dependency, Spring Boot includes an embedded Tomcat server, so you can run your application as a standalone Java application.

  1. Locate the main application file (usually named YourProjectNameApplication.java).
  2. Run it as a Java application.

Your Spring Boot application will now start, and you can access it on http://localhost:8080/ by default. If you haven't added any controllers yet, accessing this URL will result in an error. But don't worry, this means your application is up and running!

5. Create a Simple REST Endpoint:

In the src/main/java directory, under your main package, create a new class named HelloController.

package com.example.myspringbootapp;

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!";
    }
}

Run your application again and access http://localhost:8080/ to see the greeting.

This is the basic structure and setup of a Spring Boot project. From here, you can add more dependencies, create entities, services, repositories, and so on as your project requires.

  1. Creating a RESTful service in Spring Boot for beginners:

    • Description: Develop a basic RESTful service in Spring Boot.
    • Java Code:
      @RestController
      public class HelloController {
      
          @GetMapping("/hello")
          public String sayHello() {
              return "Hello, Spring Boot!";
          }
      }
      
  2. Configuring a simple web application in Spring Boot:

    • Description: Configure a basic Spring Boot web application.
    • Java Code:
      @SpringBootApplication
      public class SimpleWebApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(SimpleWebApplication.class, args);
          }
      }
      
  3. Setting up Spring Boot project structure for simplicity:

    • Description: Organize your Spring Boot project with a simple structure.
    • Structure:
      src
      ��
      ������ main
          ������ java
          ��   ������ com
          ��       ������ example
          ��           ������ SimpleWebApplication.java
          ������ resources
              ������ application.properties
      
  4. Adding dependencies to a simple Spring Boot project:

    • Description: Add necessary dependencies, such as spring-boot-starter-web, in your pom.xml.
    • XML Code:
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
      
  5. Configuring application properties in a basic Spring Boot app:

    • Description: Configure application properties, e.g., server port, in application.properties.
    • Properties (application.properties):
      server.port=8080
      
  6. Creating and running a simple Spring Boot main class:

    • Description: Create a main class to run your Spring Boot application.
    • Java Code:
      @SpringBootApplication
      public class SimpleBootApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(SimpleBootApplication.class, args);
          }
      }
      
  7. Basic controller and request mapping in Spring Boot:

    • Description: Define a basic controller with request mappings.
    • Java Code:
      @RestController
      public class SimpleController {
      
          @GetMapping("/welcome")
          public String welcome() {
              return "Welcome to Spring Boot!";
          }
      }
      
  8. Configuring logging for a straightforward Spring Boot project:

    • Description: Configure logging settings in application.properties.
    • Properties (application.properties):
      logging.level.root=info
      
  9. Securing a simple Spring Boot application for beginners:

    • Description: Apply basic security configurations for your Spring Boot app.
    • Java Code (Security Config):
      @Configuration
      @EnableWebSecurity
      public class SecurityConfig extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http.authorizeRequests()
                  .antMatchers("/public/**").permitAll()
                  .anyRequest().authenticated()
                  .and().formLogin().permitAll()
                  .and().logout().permitAll();
          }
      }