Qr Code Generation Design

In this lesson, we'll design the QR code generation functionality that will be used to create scannable tickets for event attendees.

Service Design Overview

The QR code generation process needs to be flexible and maintainable. Let's examine the key components we've established:

  • The QrCode entity has been updated to store QR codes as text in the database
  • The id field is now manually set rather than auto-generated
  • A QrCodeRepository has been created for database operations
  • A QrCodeService interface defines the core functionality
  • A QrCodeGenerationException handles error cases

Database Storage Considerations

We're storing QR codes in the database as base64 encoded strings using a TEXT column type. Here's why this approach works for our current needs:

  • The TEXT type allows for variable-length storage without the 255 character limit of VARCHAR
  • Base64 encoding lets us store binary image data as text
  • This approach requires minimal additional infrastructure

However, it's worth noting that this solution may need to be revised as the system scales, since storing images in the database can impact performance.

Error Handling

We've implemented a dedicated exception type for QR code generation failures:

public class QrCodeGenerationException extends EventTicketException { public QrCodeGenerationException() { super(); } public QrCodeGenerationException(String message) { super(message); } public QrCodeGenerationException(String message, Throwable cause) { super(message, cause); } public QrCodeGenerationException(Throwable cause) { super(cause); } public QrCodeGenerationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); } }
@ExceptionHandler(QrCodeGenerationException.class) public ResponseEntity<ErrorDto> handleQrCodeGenerationException(QrCodeGenerationException ex) { log.error("Caught QrCodeGenerationException", ex); ErrorDto errorDto = new ErrorDto(); errorDto.setError("Unable to generate QR Code"); return new ResponseEntity<>(errorDto, HttpStatus.INTERNAL_SERVER_ERROR); }

This provides clear error messages to clients while using the appropriate 500 status code, since QR code generation failures are server-side issues.

Service Interface

The QrCodeService interface is intentionally simple:

public interface QrCodeService { QrCode generateQrCode(Ticket ticket); }

Summary

  • Updated types in the QrCode entity
  • Added QrCodeGenerationException
  • Added QrCodeService
  • Added QrCodeRepository
© 2026 Devtiro Ltd. All rights reserved