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

Spring Boot - Starter Web

The spring-boot-starter-web is one of the most frequently used starters in the Spring Boot ecosystem. It's designed for building web applications, including RESTful services, with Spring MVC. The starter takes care of setting up and configuring the necessary libraries, so you can focus on writing your application without getting bogged down by the initial setup.

Key features and components bundled with the spring-boot-starter-web are:

  1. Spring MVC: The core framework for building web and RESTful applications in Spring. It provides functionalities such as request routing, content negotiation, and serialization/deserialization.

  2. Embedded Tomcat: By default, Spring Boot web applications use Tomcat as the embedded servlet container, meaning you don't need a separate server setup. Just run the application, and it will start on Tomcat.

  3. Validation: Includes Hibernate Validator, an implementation of the Java Bean Validation specification. It helps in validating the objects before they reach the application layer.

  4. Logging: Comes pre-configured with Logback for logging, but you can easily switch to other logging systems if needed.

  5. Jackson: For JSON binding. Jackson is used to convert objects to JSON and vice versa, a crucial feature for RESTful services.

  6. Path Matching and Content Negotiation: Out of the box, it can handle different types of content and URL patterns.

To use the spring-boot-starter-web, you would typically add it as a dependency in your Maven or Gradle file.

Here's how you'd include it using Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Once you've added this dependency and created a Spring Boot application, you can easily set up controllers, services, and other necessary components to create your web application. The power of Spring Boot, combined with the simplifications provided by the starter, means you can have a basic RESTful service running in just a few minutes.

If you're also interested in building web applications with reactive programming models, Spring Boot provides another starter called spring-boot-starter-webflux, which uses the WebFlux framework to enable reactive programming in Spring Boot applications.

  1. Configuring and customizing web projects with Starter Web in Spring Boot:

    • Description: Spring Boot's spring-boot-starter-web simplifies the setup of web projects by providing essential dependencies for building web applications. It includes Spring MVC and Tomcat.
    • Code:
      <!-- Example using Spring Boot Starter Web in your project's POM -->
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
          <!-- Other web-related dependencies -->
      </dependencies>
      
  2. Building web applications with Spring Boot Starter Web:

    • Description: Create web applications effortlessly using Spring Boot's auto-configuration. Define controllers, services, and views to build the application's structure.
    • Code:
      // Example controller in Spring Boot Starter Web
      @Controller
      public class HomeController {
          @GetMapping("/")
          public String home() {
              return "index"; // Returns the view named "index"
          }
      }
      
  3. RESTful services and APIs with Starter Web in Spring Boot:

    • Description: Utilize Spring MVC's @RestController and @RequestMapping annotations to develop RESTful services and APIs. Leverage HTTP methods for different operations.
    • Code:
      // Example RESTful controller in Spring Boot Starter Web
      @RestController
      @RequestMapping("/api")
      public class ApiController {
          @GetMapping("/resource")
          public String getResource() {
              return "Resource Data";
          }
      }
      
  4. Web MVC and Thymeleaf templates with Spring Boot Starter Web:

    • Description: Combine Spring MVC with Thymeleaf for server-side rendering. Create dynamic web pages using Thymeleaf's template engine.
    • Code:
      <!-- Example Thymeleaf template in Spring Boot Starter Web -->
      <!DOCTYPE html>
      <html lang="en" xmlns:th="http://www.thymeleaf.org">
      <head>
          <meta charset="UTF-8">
          <title>Thymeleaf Example</title>
      </head>
      <body>
          <h1 th:text="${message}">Default Message</h1>
      </body>
      </html>
      
  5. Securing web applications with Spring Boot Starter Web:

    • Description: Use Spring Security to secure web applications. Configure security settings and annotations to control access to different parts of the application.
    • Code:
      // Example security configuration in Spring Boot Starter Web
      @EnableWebSecurity
      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();
          }
      }
      
  6. Testing web applications with Starter Web in Spring Boot:

    • Description: Leverage the @WebMvcTest annotation to focus on testing the web layer of your application. Use MockMvc to perform HTTP requests and validate responses.
    • Code:
      // Example web application test with Spring Boot Starter Web
      @RunWith(SpringRunner.class)
      @WebMvcTest(HomeController.class)
      public class HomeControllerTest {
          @Autowired
          private MockMvc mockMvc;
      
          @Test
          public void testHomePage() throws Exception {
              mockMvc.perform(get("/"))
                     .andExpect(status().isOk())
                     .andExpect(view().name("index"));
          }
      }