List Published Event Endpoint
In this lesson, we'll create an endpoint that allows anyone to view published events, enabling potential attendees to browse available events without needing to log in first.
Creating the Published Events Controller
Let's create a dedicated controller for published events, keeping it separate from our existing event management endpoints.
@RestController
@RequestMapping(path = "/api/v1/published-events")
@RequiredArgsConstructor
public class PublishedEventController {
private final EventService eventService;
private final EventMapper eventMapper;
@GetMapping
public ResponseEntity<Page<ListPublishedEventResponseDto>> listPublishedEvents(Pageable pageable) {
// Map the events to DTOs and return them in the response
return ResponseEntity.ok(eventService.listPublishedEvents(pageable)
.map(eventMapper::toListPublishedEventResponseDto));
}
}Configuring Public Access
To make the endpoint accessible without authentication, we need to update our security configuration.
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(
HttpSecurity http,
UserProvisioningFilter userProvisioningFilter) throws Exception {
http
.authorizeHttpRequests(authorize ->
authorize
// Allow public access to published events
.requestMatchers(HttpMethod.GET, "/api/v1/published-events").permitAll()
// All other endpoints require authentication
.anyRequest().authenticated())
// ... rest of the configuration
return http.build();
}
}Summary
- Created the
PublishedEventControllerclass - Implemented the list published events endpoint
- Made the list published events endpoint public