Spring Boot is a framework based on Java (Spring Framework) that helps developers quickly build applications. In this course, we will step-by-step explore how to start backend development using Spring Boot and explain how to verify that the developed application is functioning correctly.
1. What is Spring Boot?
Spring Boot is designed to simplify the complex setup and configuration of the existing Spring Framework, allowing developers to easily start projects. This increases productivity and helps developers quickly create microservices.
- Autoconfiguration: Automatically applies basic configurations, so developers do not have to spend time on detailed settings.
- Starter Dependencies: Provides starter packages that help easily add the basic dependencies needed to start a Spring Boot project.
- Embedded Server: Supports embedded servers such as Tomcat and Jetty, allowing applications to run without separate server configuration.
2. Setting Up the Development Environment
2.1 Installing Required Tools
To start a Spring Boot project, you need JDK, an IDE, and Maven or Gradle. Here are the setup methods:
- Install JDK: Install Oracle JDK or OpenJDK. JDK 11 or higher is required.
- Install IDE: Download and install your preferred IDE such as IntelliJ IDEA, Eclipse, or STS.
- Install Maven or Gradle: Install Maven or Gradle for dependency management. Using built-in support from the IDE is also recommended.
2.2 Creating a Project
You can easily create a Spring Boot project using Spring Initializr.
- Access Spring Initializr.
- Input the project metadata (group, artifact, name, etc.).
- Select the Spring Boot version and add the required starter dependencies.
- Click the PROJECT button to download the ZIP file.
- Extract the downloaded file and open it in your IDE.
3. Configuring the Basic Application
3.1 Creating a Controller
Once the application is successfully created, let’s create a basic REST API. Below is an example of a controller that returns Hello, World!.
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
}
3.2 Creating a Service Layer
The service layer handles the business logic. Below is an example of a simple service class.
@Service
public class GreetingService {
public String greet() {
return "Greetings from the Service Layer!";
}
}
3.3 Database Integration
Spring Boot allows easy integration with databases through JPA. We will proceed with the example using the H2 database.
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
3.4 Application Properties Configuration
You can configure the database connection information in the application.properties file.
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.enabled=true
4. Running the Application
You can start the application by running the Main class within the IDE. By default, the embedded Tomcat server runs, and you can access http://localhost:8080/api/hello
to verify the Hello, World! message.
5. Verifying Functionality
5.1 Installing and Configuring Postman
Use Postman to test the API. After installing Postman, send a request as follows:
- Open Postman and create a new request.
- Select the GET method and enter
http://localhost:8080/api/hello
in the URL. - Click the Send button to send the request. Check if “Hello, World!” appears in the response.
5.2 Testing with JUnit
You can perform tests on the code you have written using JUnit. Below is an example of a unit test.
@RunWith(SpringRunner.class)
@SpringBootTest
public class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHello() throws Exception {
mockMvc.perform(get("/api/hello"))
.andExpect(status().isOk())
.andExpect(content().string("Hello, World!"));
}
}
6. Conclusion
You have learned how to build a simple backend application using Spring Boot and verify its functionality. I hope this course has helped you understand the basic configuration and structure of Spring Boot. In the future, try to develop mature applications by applying more complex features and design patterns.