Spring Boot Tutorial
Spring Boot is a framework for building Java-based applications. It simplifies the setup of new applications by providing default configurations and minimizing the need for boilerplate code. Spring Boot is part of the larger Spring framework ecosystem, often used for creating REST APIs, microservices, and web applications.
1. Setting Up a Spring Boot Projectβ
Using Spring Initializrβ
- Go to Spring Initializr.
- Choose your project settings, such as project type (Maven or Gradle), Java version, and dependencies.
- Generate and download the project.
- Extract the project files and open the project in your preferred IDE.
Adding Dependenciesβ
In the pom.xml (for Maven projects), add dependencies as needed, such as:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. Basic Application Structureβ
A typical Spring Boot application has the following structure:
src/main/java
βββ com.example.demo
β βββ DemoApplication.java // Main application class
β βββ controller // Package for controllers
β βββ HelloController.java
βββ src/main/resources
βββ application.properties // Configuration file
βββ static // Static resources
Main Application Classβ
The main application class is annotated with @SpringBootApplication and contains the main method.
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);
}
}
3. Building a REST Controllerβ
Create a simple REST controller to handle HTTP requests.
Exampleβ
Create a HelloController in the controller package:
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
Explanationβ
- @RestController: Marks this class as a REST controller.
- @RequestMapping: Maps URL paths to this controller.
- @GetMapping: Maps GET requests to
sayHello.
4. Application Propertiesβ
The application.properties file in src/main/resources allows you to configure application settings.
Exampleβ
server.port=8081
spring.application.name=MySpringApp
This example changes the default port to 8081 and sets the application name.
5. Working with Spring Boot Data JPAβ
Spring Data JPA simplifies database operations. Start by adding the JPA dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
Example Entityβ
Define an Entity class:
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
}
Example Repositoryβ
Create a repository interface:
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
6. Using Spring Boot DevToolsβ
Add the following dependency for hot reloading and faster development:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
7. Spring Boot Testingβ
Spring Boot makes testing simple with Springβs testing support.
Example Test Caseβ
package com.example.demo;
import com.example.demo.controller.HelloController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private HelloController helloController;
@Test
void contextLoads() {
assertThat(helloController).isNotNull();
}
}
8. Building and Running the Applicationβ
To build and run the application:
-
Build: Use the following Maven command:
mvn clean install -
Run: Start the application using:
mvn spring-boot:run
9. Packaging the Applicationβ
You can package the Spring Boot application into a JAR file:
mvn clean package
The JAR file will be generated in the target/ directory. Run it with:
java -jar target/demo-0.0.1-SNAPSHOT.jar
Summaryβ
This tutorial covered Spring Boot basics:
- Setting up a Spring Boot project with Spring Initializr.
- Creating REST controllers to handle HTTP requests.
- Configuring application settings using
application.properties. - Working with Spring Data JPA to connect to a database.
- Testing and running the application.
Spring Boot is a powerful framework that simplifies the development of Java applications, making it easy to build, deploy, and manage complex applications.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.