Spring Boot REST API with H2, Spring Data JPA, and Complete CRUD Operations #195
Replies: 9 comments
-
Beta Was this translation helpful? Give feedback.
-
|
Here's a step-by-step guide to integrate SpringDoc Swagger UI into your existing Spring Boot application and generate OpenAPI docs automatically from annotations: ✅ Step 1: Add SpringDoc DependencyIn your <dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>✅ Step 2: Verify Spring Boot CompatibilityEnsure your project uses Spring Boot 3.x (for SpringDoc 2.x) or Spring Boot 2.x (for SpringDoc 1.x).
✅ Step 3: Add Basic Config (Optional)Create a configuration class if you want to customize metadata. package com.example.config;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI enterpriseOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("Enterprise Product API")
.description("API documentation for Product Management")
.version("1.0.0"));
}
@Bean
public GroupedOpenApi productApi() {
return GroupedOpenApi.builder()
.group("products")
.pathsToMatch("/api/products/**")
.build();
}
}✅ Step 4: Annotate Your Controller and Modelimport io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
@RestController
@RequestMapping("/api/products")
@Tag(name = "Product APIs", description = "CRUD operations for Products")
public class ProductController {
@GetMapping
@Operation(summary = "Get all products")
public List<Product> getAllProducts() {
...
}
@PostMapping
@Operation(summary = "Add a new product")
public Product addProduct(@RequestBody Product p) {
...
}
// More endpoints...
}Also add import io.swagger.v3.oas.annotations.media.Schema;
@Schema(description = "Product Entity")
public class Product {
@Schema(description = "Unique ID", example = "101")
private Long id;
@Schema(description = "Product name", example = "Laptop")
private String name;
...
}✅ Step 5: Add Actuator (Optional, but useful)<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>✅ Step 6: Run Your Application
✅ Pro Tips
|
Beta Was this translation helpful? Give feedback.
-
|
Here’s a fully annotated sample controller for your Spring Boot + SpringDoc Swagger setup, crafted to demonstrate enterprise-grade API design and documentation. 📁 File:
|
Beta Was this translation helpful? Give feedback.
-
|
Here's a clean and production-inspired setup to integrate: 🧾 1.
|
Beta Was this translation helpful? Give feedback.
-
|
Here’s how to evolve your Spring Boot REST API into a more enterprise-grade and layered architecture by integrating: 🔌 1. DTOs + ModelMapper IntegrationWhy?
✅ Steps:
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.1.1</version>
</dependency>
public class ProductDTO {
private Long id;
private String name;
private String category;
private double price;
}
@Configuration
public class MapperConfig {
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
@Autowired
private ModelMapper modelMapper;
public ProductDTO convertToDto(Product product) {
return modelMapper.map(product, ProductDTO.class);
}
public Product convertToEntity(ProductDTO dto) {
return modelMapper.map(dto, Product.class);
}
|
Beta Was this translation helpful? Give feedback.
-
|
Here is the downloadable ZIP file containing a structured Spring Boot project with the following enterprise-ready setup: ✅ Folder structure for controller, dto, exception, model, repository, service, config |
Beta Was this translation helpful? Give feedback.
-
|
This is the final version of the project. This is an enhanced Spring Boot enterprise project ready with:
✅ Now adding Swagger/OpenAPI annotations to the controllers and services. We have:
We’ll repackage the entire enhanced application with Swagger support so you can download and run it. 🚀 Your Swagger-enabled, enterprise-grade Spring Boot REST API is ready for download: 👉 Download the project - product-api-enterprise-swagger.zip To use Swagger UI:
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
PRASETHA N |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Spring Boot REST API with H2, Spring Data JPA, and Complete CRUD Operations
🔧 Overview
This application demonstrates a complete CRUD Spring Boot REST API with:
data.sqlfor seeding📁 Project Structure
📦 Dependencies (
pom.xml)⚙
application.properties🌟 Entity (
Product.java)🎯 DTO (
ProductDTO.java)📚 Repository
🧠 Service Layer
Interface
Implementation
🎛 Controller
🛑 Exception Handling
💾 Data Seeding (
data.sql)🚀 Run the App
mvn spring-boot:runspringdoc-openapidependency)Beta Was this translation helpful? Give feedback.
All reactions