Skip to main content

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:

  1. Receive incoming HTTP POST requests at the /compare endpoint.
  2. Validate and extract the multipart files (baseFiles, compareFile) and the optional region JSON string.
  3. Delegate the core business logic (comparison) to the CompareService.
  4. Handle potential exceptions and return a consistent ResponseEntity.
DetailValue
Class NameCompareController
Endpoint/compare
MethodPOST
PurposeAPI entry point for image comparison.
SecurityRequires X-API-KEY header.

2. Spring Boot and Annotation Breakdown​

AnnotationTypePurpose
@RestControllerClassMarks the class as a Spring controller where methods return JSON/XML responses directly (combines @Controller and @ResponseBody).
@RequestMapping("/compare")ClassMaps all methods in this controller to the base path /compare.
@AutowiredFieldInjects the CompareService dependency, enabling access to the comparison logic.
@PostMappingMethodMaps the compareImages method to handle HTTP POST requests at the base path (/compare).
@RequestPartParameterBinds parts of a multipart request to the method parameters (used for MultipartFiles).
@RequestParamParameterBinds 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.

AnnotationPurpose
@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.
@ApiResponseExplicitly 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.StepActionCode Implication
1LoggingLogs the start of the comparison request, including the number of baseFiles received.logger.info("Received request to compare {} base files", baseFiles.length);
2DelegationCalls 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);
3Error HandlingIf any exception occurs during the comparison process (e.g., file IO error, OpenCV processing error), the catch block executes.catch (Exception e)
4Error ResponseLogs 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:

  1. CompareService: Injected via @Autowired, this is the service responsible for executing the actual OpenCV image comparison logic.
  2. ResponseHandler: A static utility used to format and return standardized error responses, maintaining API consistency.