Ticket Qrcode Endpoint
In this lesson, we implement the endpoint to get the QR code image.
Creating the QR Code Endpoint
We'll add a new endpoint to the TicketController that returns a QR code image associated with a specific ticket.
The endpoint will be an extension of the existing get ticket functionality:
@GetMapping(path = "/{ticketId}/qr-codes")
public ResponseEntity<byte[]> getTicketQrCode(
@AuthenticationPrincipal Jwt jwt,
@PathVariable UUID ticketId
) {
byte[] qrCodeImage = qrCodeService.getQrCodeImageForUserAndTicket(
parseUserId(jwt),
ticketId
);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
headers.setContentLength(qrCodeImage.length);
return ResponseEntity.ok()
.headers(headers)
.body(qrCodeImage);
}HTTP Headers for Image Response
To properly serve the QR code image, we need to set specific HTTP headers:
- We set the
Content-Typeheader toimage/pngsince we're returning a PNG image - We set the
Content-Lengthheader to match the size of our image data in bytes
These headers help the client browser understand how to handle and display the received data correctly.
Summary
- Implemented the get QR Code endpoint