Spring Boot Backend Development Course, Spring Boot 3 and Java Version

Hello! Today, we will conduct an in-depth course on backend development using Spring Boot. Spring Boot is a Java-based framework that provides a platform for developing web applications quickly and easily. This course focuses on Spring Boot 3 and the latest version of Java.

1. What is Spring Boot?

Spring Boot is an extension of the Spring Framework. The Spring Framework is a powerful development tool for applications developed in Java, but its configuration and initialization can be complex. Spring Boot was created to address these issues and provides the following key features:

  • Auto Configuration: You can start your application without complex Spring configurations.
  • Standalone Applications: It provides applications that can run independently through an embedded server.
  • Production Ready: It includes various features for monitoring and management.

2. Key Changes in Spring Boot 3

Spring Boot 3 is based on Spring Framework 6 and includes several new features and improvements. Key changes include:

  • JDK 17 Required: Spring Boot 3 requires a minimum of JDK 17, allowing the use of new Java features.
  • HTTP/2 Support: Supports HTTP/2 by default for better performance.
  • Jakarta EE 9 Support: The package namespace has changed from `javax` to `jakarta`.

3. Setting Up the Development Environment

To develop with Spring Boot, you need to set up your development environment. Please follow the steps below.

1. Install JDK: Install OpenJDK 17 or higher.
2. Install an IDE: Choose and install an IDE such as IntelliJ IDEA or Eclipse.
3. Install Maven or Gradle: Use Maven or Gradle to manage dependencies.

3.1 Install JDK

The JDK is a Java development tool; download and install the latest version. After installation, set the PATH environment variable.

3.2 Install IDE

IntelliJ IDEA is recommended due to its good integration with Spring Boot. Add the required plugins after installation.

3.3 Install Maven or Gradle

Choose one to install. If using Maven, manage dependencies through pom.xml. If using Gradle, use the build.gradle file.

4. Creating a Spring Boot Project

There are various ways to create a Spring Boot project. Here, we will introduce a method using Spring Initializr.

  1. Visit the Spring Initializr website.
  2. Enter Project Metadata:
    • Group: com.example
    • Artifact: demo
    • Dependencies: Optionally add Spring Web, Spring Data JPA, H2 Database, etc.
  3. Click the Generate button to download the ZIP file.
  4. Extract the downloaded ZIP file and open the project in your IDE.

5. Creating a Simple RESTful API

Now, let’s create a simple RESTful API using Spring Boot. The following example is a user management API.

5.1 Add Dependency

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

5.2 Create Model Class

package com.example.demo.model;

public class User {
    private Long id;
    private String name;
    
    // Constructor, Getter, and Setter methods
}

5.3 Create Controller Class

package com.example.demo.controller;

import org.springframework.web.bind.annotation.*;
import com.example.demo.model.User;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/users")
public class UserController {
    private List users = new ArrayList<>();

    @GetMapping
    public List getUsers() {
        return users;
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        users.add(user);
        return user;
    }
}

5.4 Run the Application

Now when you run the application, you can check the user list at http://localhost:8080/api/users.

6. Integrating with a Database

Spring Boot can integrate with various databases. You can use several databases like H2, MySQL, PostgreSQL, etc. Here, we will use H2.

6.1 Add H2 Database Dependency

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

6.2 Configure application.properties

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

7. Writing API Documentation

As projects grow, writing API documentation becomes essential. You can easily write API documentation using Swagger.

7.1 Add Swagger Dependency

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

7.2 Create Swagger Configuration Class

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

7.3 Using Swagger UI

After running the application, you can check the API documentation through Swagger UI at http://localhost:8080/swagger-ui/.

8. Writing Test Cases

Below is an example of writing test cases using Spring Boot. We will use JUnit and Spring Test to do this.

8.1 Add Dependency

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

8.2 Create Test Class

package com.example.demo;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTests {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void getUsers_ShouldReturnUsers() throws Exception {
        this.mockMvc.perform(get("/api/users"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.length()").value(0));
    }
}

9. Conclusion

In this course, we explored how to create a simple backend application using Spring Boot. Thanks to its various features and easy configuration, Spring Boot is a useful tool for quickly developing web applications. For deeper learning, please refer to the official Spring Boot documentation.

Thank you!