Search Controller Updates

In this lesson, we'll enhance the published events controller to support optional search functionality, allowing users to find events by searching through event names and venues.

Understanding Optional Search Parameters

The @RequestParam annotation in Spring Boot lets us add optional parameters to our endpoints.

By setting required = false, we tell Spring that the parameter is optional, meaning the endpoint will work both with and without the search parameter.

Here's how we update our controller:

@GetMapping public ResponseEntity<Page<ListPublishedEventResponseDto>> listPublishedEvents( @RequestParam(required = false) String q, Pageable pageable) { Page<Event> events; if(null != q && !q.trim().isEmpty()) { events = eventService.searchPublishedEvents(q, pageable); } else { events = eventService.listPublishedEvents(pageable); } return ResponseEntity.ok( events.map(eventMapper::toListPublishedEventResponseDto) ); }

Search Flow Implementation

The controller now handles two different scenarios:

  • When a search query is provided, it calls searchPublishedEvents with the query
  • When no search query is provided, it calls listPublishedEvents to show all published events

The code checks if the query parameter q exists and isn't empty after trimming whitespace.

We use the query parameter name q as it's a common convention in search APIs and keeps our URLs clean and readable.

Summary

  • Added the search published events endpoint the PublishedEventsController
© 2026 Devtiro Ltd. All rights reserved