CompareController
CompareController.java
This document explains the functionality, structure, and integration of the CompareController class within the Image Comparison API.
1. Overview and Purpose
The CompareController acts as the Controller Layer entry point for the image comparison microservice. Its primary responsibility is to:
- Receive incoming HTTP
POSTrequests at the/compareendpoint. - Validate and extract the multipart files (
baseFiles,compareFile) and the optionalregionJSON string. - Delegate the core business logic (comparison) to the
CompareService. - Handle potential exceptions and return a consistent
ResponseEntity.
| Detail | Value |
|---|---|
| Class Name | CompareController |
| Endpoint | /compare |
| Method | POST |
| Purpose | API entry point for image comparison. |
| Security | Requires X-API-KEY header. |
2. Spring Boot and Annotation Breakdown
| Annotation | Type | Purpose |
|---|---|---|
@RestController | Class | Marks the class as a Spring controller where methods return JSON/XML responses directly (combines @Controller and @ResponseBody). |
@RequestMapping("/compare") | Class | Maps all methods in this controller to the base path /compare. |
@Autowired | Field | Injects the CompareService dependency, enabling access to the comparison logic. |
@PostMapping | Method | Maps the compareImages method to handle HTTP POST requests at the base path (/compare). |
@RequestPart | Parameter | Binds parts of a multipart request to the method parameters (used for MultipartFiles). |
@RequestParam | Parameter | Binds a query parameter (region) to the method parameter. required = false makes it optional. |
3. Open API (Swagger) Integration
The controller is fully annotated to integrate with springdoc-openapi, generating interactive and secure API documentation.
| Annotation | Purpose |
|---|---|
@Tag(name = "Image Comparison", ...) | Groups this controller's endpoints under the "Image Comparison" category in the Swagger UI. |
@SecurityRequirement(name = "apiKeyScheme") | Enforces API Key security for all endpoints in this controller, causing a lock icon to appear next to the endpoint in Swagger UI. |
@Operation(...) | Provides a detailed summary, description, and defines expected HTTP status codes and responses for the POST /compare endpoint. |
@ApiResponse | Explicitly documents the possible HTTP response codes (200, 400, 401, 500) for client consumption. |
4. compareImages Method Logic
The main method handles the core request flow:
Java
public ResponseEntity<?> compareImages(...)
| S.No. | Step | Action | Code Implication |
|---|---|---|---|
| 1 | Logging | Logs the start of the comparison request, including the number of baseFiles received. | logger.info("Received request to compare {} base files", baseFiles.length); |
| 2 | Delegation | Calls the compareService.compare() method, passing all request parts. The service returns the final ResponseEntity (which is often a JSON structure prepared by ResponseHandler). | return compareService.compare(baseFiles, compareFile, regionJson); |
| 3 | Error Handling | If any exception occurs during the comparison process (e.g., file IO error, OpenCV processing error), the catch block executes. | catch (Exception e) |
| 4 | Error Response | Logs the error details and uses the utility ResponseHandler.error() to generate a consistent ResponseEntity (typically HTTP 500 Internal Server Error) containing the failure message. | logger.error(...) and return ResponseHandler.error(...) |
5. Dependency
The controller relies on two key dependencies:
CompareService: Injected via@Autowired, this is the service responsible for executing the actual OpenCV image comparison logic.ResponseHandler: A static utility used to format and return standardized error responses, maintaining API consistency.