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
Creating a basic Spring Boot application is a straightforward process, especially with the help of the Spring Initializr tool. Here's a step-by-step guide to creating a simple web application:
Using Spring Initializr:
Using IDE:
Many modern IDEs (like IntelliJ IDEA, Eclipse, and STS) have built-in support for bootstrapping a Spring Boot project using Spring Initializr.
Extract the zip file you downloaded and open the project in your preferred IDE (IntelliJ IDEA, Eclipse, etc.).
To handle incoming HTTP requests, create a simple controller:
package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } }
Spring Boot applications contain an embedded Tomcat server, so you don't need to deploy your application to a separate server.
In the main class (named something like DemoApplication.java
), there's a main
method that starts the Spring Boot application:
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Run this main
method either directly from your IDE or from the command line using Maven or Gradle:
For Maven:
mvn spring-boot:run
For Gradle:
./gradlew bootRun
Once your application is running, open a web browser or use a tool like curl to access your endpoint:
http://localhost:8080/hello
You should see the message "Hello, Spring Boot!".
From this basic setup, you can expand your application by:
application.properties
or application.yml
file.Spring Boot provides a lot of auto-configuration and sensible defaults, making it easy to get started and build robust applications.
Building a Hello World application with Spring Boot:
@RestController public class HelloWorldController { @GetMapping("/hello") public String sayHello() { return "Hello, World!"; } }
Creating a RESTful service in Java with Spring Boot:
@RestController @RequestMapping("/api") public class RestfulController { @GetMapping("/greet") public String greet() { return "Greetings!"; } // Other RESTful endpoints }
Configuring Spring Boot application properties for beginners:
application.properties
or application.yml
file.# application.properties server.port=8080
Basic controller and request mapping in Spring Boot:
@Controller public class BasicController { @RequestMapping("/welcome") public String welcome() { return "Welcome to Spring Boot!"; } }
Adding dependencies and dependencies management in Spring Boot:
pom.xml
or build.gradle
file to include dependencies.<!-- Example for Maven --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
Setting up a basic web application in Spring Boot:
@Controller public class WebController { @GetMapping("/home") public String home() { return "index"; } }
Creating a simple data model in Spring Boot:
@Entity public class Item { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; // Other fields, getters, and setters }
Handling static resources in a Spring Boot project:
src/main/resources/static
directory.<!-- Example usage in HTML --> <link rel="stylesheet" href="/css/style.css">
Configuring logging in a basic Spring Boot application:
application.properties
or application.yml
file.# application.properties logging.level.root=INFO
Adding database connectivity to a Spring Boot app:
@Entity public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String email; // Other fields, getters, and setters }
Configuring application profiles in Spring Boot:
application.properties
or application.yml
file.# application-dev.properties server.port=8081
Implementing error handling in a Spring Boot project:
@ControllerAdvice
class with exception handling methods.@ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong"); } }
Securing a basic Spring Boot application for beginners:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage("/login").permitAll() .and() .logout().permitAll(); } }
Unit testing basics for Spring Boot applications:
@RunWith(SpringRunner.class) @SpringBootTest public class MyServiceTest { @Autowired private MyService myService; @Test public void testSomething() { // Test logic } }