DEV Community

Cover image for Building a RESTful API with Spring Boot: A Step-by-Step Tutorial
Edson Camacho
Edson Camacho

Posted on

Building a RESTful API with Spring Boot: A Step-by-Step Tutorial

Spring Boot is one of the most popular frameworks for building Java-based applications. Its simplicity and productivity features make it a go-to choice for developers. In this tutorial, we will walk through the process of building a RESTful API using Spring Boot, from setting up the project to implementing a basic API endpoint.

Setting Up the Project
To begin, we need to set up our Spring Boot project. A quick way to do this is by using Spring Initializr. Head over to start.spring.io and generate a new project with the following dependencies:

Spring Web
Spring Data JPA
H2 Database (for simplicity)
Once the project is generated, open it in your preferred IDE, such as IntelliJ IDEA or Eclipse, and you’re ready to start coding.

Creating the Model Class
Start by creating a model class for the API. This model will represent an entity in our database, which, in this case, is a Product.

`@Entity
public class Product {
@id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;

// Getters and Setters
Enter fullscreen mode Exit fullscreen mode

}`

Creating the Repository Interface
Next, we need to create a repository interface to handle CRUD operations for our Product entity. Spring Data JPA will simplify this process for us.

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
}

Creating the Service Layer
We then create the service layer, which will handle the business logic. The service will interact with the repository to fetch data from the database.

`@Service
public class ProductService {
private final ProductRepository productRepository;

@Autowired
public ProductService(ProductRepository productRepository) {
    this.productRepository = productRepository;
}

public List<Product> getAllProducts() {
    return productRepository.findAll();
}
Enter fullscreen mode Exit fullscreen mode

}`

Creating the Controller
Now, let’s create the controller that will expose a REST API endpoint for retrieving the list of products. This controller will handle HTTP requests and respond with the appropriate data.

`@RestController
@RequestMapping("/api/products")
public class ProductController {
private final ProductService productService;

@Autowired
public ProductController(ProductService productService) {
    this.productService = productService;
}

@GetMapping
public List<Product> getAllProducts() {
    return productService.getAllProducts();
}
Enter fullscreen mode Exit fullscreen mode

}`

Running the Application
With everything in place, you can now run the Spring Boot application. Once the app is running, navigate to http://localhost:8080/api/products in your browser or use an API tool like Postman to test the endpoint. You should see a JSON response containing a list of products.

For more in-depth tutorials and tips, feel free to check out my blog, where I cover a variety of topics on Java, Spring Boot, and software development.

Conclusion
In this tutorial, we’ve walked through the basics of creating a RESTful API with Spring Boot. We set up a project, created a model and repository, added a service layer, and exposed a simple endpoint. With Spring Boot, you can quickly get up and running with REST APIs, making it an ideal choice for building Java-based applications.

About Me
Hi! I’m Edson Camacho, a passionate full-stack developer and instructor. I specialize in Java, Spring Boot, and web development. I enjoy teaching others and sharing valuable insights through my blog and online courses. You can find more tutorials, tips, and resources on my blog Software Engineer's Academy.

Top comments (1)

Collapse
 
edson_camacho_96de9a185d3 profile image
Edson Camacho

On my blog, I dive deeper into programming with Java, Spring Boot, Angular, and MySQL. I would be happy to help you in your development journey.