Spring Boot is a powerful framework for building production-ready applications with minimal configuration. It simplifies the development process by providing default configurations and reducing the amount of boilerplate code you need to write. In this tutorial, we will walk through creating your first Spring Boot application from scratch.
Before you start, ensure you have the following installed on your machine:
Spring Initializr is a web-based tool that helps you bootstrap your new Spring Boot project. It provides a simple interface to configure your project's dependencies and generates the necessary files.
Access Spring Initializr:
Configure Your Project:
Dependencies:
Generate Project:
Import the Project into Your IDE:
File > New > Project from Existing Sources.If you prefer using the command line, you can install the Spring CLI and generate a project with it.
Install Spring CLI:
Generate Project:
spring init --dependencies=web,data-jpa,h2 \
--build=maven \
--language=java \
--name=demo \
--package-name=com.example.demo \
my-spring-boot-app
Import the Project into Your IDE:
After generating your project, you will see a standard Maven project structure. Here are some key files and directories:
src/main/java/com/example/demo: Contains your Java source code.src/main/resources: Contains configuration files like application.properties.pom.xml (for Maven) or build.gradle (for Gradle): Project build configuration.Let's create a simple REST controller to handle HTTP requests.
Create a New Java Class:
src/main/java/com/example/demo.controller.controller package, create a new Java class named HelloController.Implement the Controller:
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!";
}
}
@Controller and @ResponseBody, making it easier to create RESTful web services.Spring Boot applications can be run directly from your IDE or using the command line.
Run the Main Class:
DemoApplication.java in src/main/java/com/example/demo.Access the Application:
Navigate to Project Directory:
cd path/to/your/project
Build and Run the Application:
mvn spring-boot:run
Access the Application:
Let's create a simple entity class to represent data in our application.
Create a New Java Class:
src/main/java/com/example/demo.model.model package, create a new Java class named User.Implement the Entity:
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;
private String email;
// Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Let's create a repository interface to interact with the database.
Create a New Java Interface:
src/main/java/com/example/demo.repository.repository package, create a new Java interface named UserRepository.Implement the Repository:
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> {
// No need to write any methods; Spring Data JPA provides them automatically.
}
Let's create a new controller to handle user-related HTTP requests.
Create a New Java Class:
src/main/java/com/example/demo.controller.controller package, create a new Java class named UserController.Implement the Controller:
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public List<User> getAllUsers() {
return userRepository.findAll();
}
@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}
UserRepository dependency.You can test your application using tools like Postman or curl.
Get All Users:
curl http://localhost:8080/api/users
Create a New User:
curl -X POST http://localhost:8080/api/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john.doe@example.com"}'
In this tutorial, we have created a simple Spring Boot application with a REST controller and a database entity. We explored the project structure, set up dependencies, and implemented basic CRUD operations. This should give you a solid foundation to build more complex applications using Spring Boot.
application.properties or application.yml.By following these best practices, you can develop robust and maintainable Spring Boot applications.