๐ CRUD Operations
Get full REST API with zero boilerplate code! The @Crud annotation automatically generates all standard CRUD endpoints for your entities.
โก Quick Startโ
1. Create Your Entityโ
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
private Double price;
private Integer stock;
// Constructors, getters, setters...
}
2. Create Repositoryโ
@Component
public interface ProductRepository extends BaseRepository<Product, Long> {
// Framework handles all basic operations automatically
}
3. Add @Crud Controllerโ
@Component
@Crud(
entity = Product.class,
endpoint = "/api/products",
enablePagination = true,
enableSearch = true,
searchableFields = {"name", "description"}
)
public class ProductController {
// That's it! All CRUD endpoints are auto-generated
}
4. Start Using Your APIโ
๐ You instantly get these endpoints:
| Method | Endpoint | Description |
|---|---|---|
GET | /api/products/{id} | Get product by ID |
POST | /api/products | Create new product |
PUT | /api/products/{id} | Update product |
DELETE | /api/products/{id} | Delete product |
GET | /api/products/search?name=laptop | Search products |
๐ @Crud Configurationโ
@Crud(
entity = Product.class, // Your JPA entity
endpoint = "/api/products", // Base URL path
enablePagination = true, // Adds ?page=1&size=10 support
enableSearch = true, // Adds /search endpoint
searchableFields = {"name", "description"}, // Fields to search in
enableExists = true // Adds /exists/{id} endpoint
)
Configuration Optionsโ
| Option | Default | Description |
|---|---|---|
entity | Required | The JPA entity class |
endpoint | Required | Base URL path for endpoints |
enablePagination | false | Enable pagination support |
enableSearch | false | Enable search functionality |
searchableFields | {} | Fields to search in (when search enabled) |
enableExists | false | Enable exists check endpoint |
๐จ Customizing Methodsโ
Need custom logic? Simply override any method:
@Component
@Crud(entity = Product.class, endpoint = "/api/products")
public class ProductController {
private final ProductRepository productRepository;
public ProductController(ProductRepository productRepository) {
this.productRepository = productRepository;
}
// Override findAll to show only in-stock products
@CrudOverride(value = "Returns only in-stock products")
public Response findAll(Request request) {
var inStockProducts = productRepository.findAll().stream()
.filter(product -> product.getStock() > 0)
.toList();
return Response.json(ApiResponse.success("In-stock products retrieved", inStockProducts));
}
// All other methods (findById, create, update, delete) remain auto-generated
}
Manual Route Registration for Overridesโ
When you override a method, register its route manually:
public static void main(String[] args) {
Router router = new Router();
// Manual registration for overridden methods
router.GET("/api/products", "findAll", ProductController.class);
Server server = new Server(router, config);
server.start(8080);
}
๐งช Testing Your APIโ
Create Productโ
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"Gaming Laptop","description":"High-performance laptop","price":1299.99,"stock":5}' \
"http://localhost:8080/api/products"
Get All Productsโ
curl "http://localhost:8080/api/products"
Get Product by IDโ
curl "http://localhost:8080/api/products/1"
Search Productsโ
curl "http://localhost:8080/api/products/search?name=laptop"
Update Productโ
curl -X PUT -H "Content-Type: application/json" \
-d '{"name":"Updated Laptop","description":"Updated description","price":1199.99,"stock":3}' \
"http://localhost:8080/api/products/1"
Delete Productโ
curl -X DELETE "http://localhost:8080/api/products/1"
๐ Response Formatโ
All CRUD endpoints return standardized responses:
{
"success": true,
"message": "Product retrieved successfully",
"data": {
"id": 1,
"name": "Gaming Laptop",
"description": "High-performance laptop",
"price": 1299.99,
"stock": 5
},
"timestamp": "2023-12-07T10:30:00"
}
๐ง Requirementsโ
Before using @Crud, ensure you have:
- Entity Class: JPA annotated entity
- Repository: Interface extending
BaseRepository<Entity, ID> - Component Registration: Repository marked with
@Component - Database Configuration: Hibernate/JPA properly configured
๐ก Best Practicesโ
โ Do'sโ
- Use meaningful entity and endpoint names
- Enable search for user-facing entities
- Override methods only when you need custom logic
- Use
@CrudOverridefor documentation
โ Don'tsโ
- Don't forget manual route registration for overrides
- Don't enable unnecessary features (keeps API clean)
- Don't override methods unless you need custom behavior
๐ Complete Exampleโ
Check out the Simple CRUD Example for a working implementation with:
- โ Product entity with validation
- โ Repository implementation
- โ Controller with one custom method
- โ Complete test commands
- โ Real-world usage patterns
๐ฏ Result: With just a few annotations, you get a complete REST API with all CRUD operations, search, pagination, and standardized responses!