java 27 lines · 6 steps

Binding collection query params in Spring

A Spring REST endpoint maps repeated and comma-separated query parameters into typed collections and folds them into a filter.

Explained by highlit
1@RestController
2@RequestMapping("/api/products")
3public class ProductSearchController {
4 
5 private final ProductQueryService productQueryService;
6 
7 public ProductSearchController(ProductQueryService productQueryService) {
8 this.productQueryService = productQueryService;
9 }
10 
11 @GetMapping
12 public List<ProductView> search(
13 @RequestParam(name = "tags", required = false) List<String> tags,
14 @RequestParam(name = "ids", defaultValue = "") Set<Long> ids,
15 @RequestParam(name = "status", required = false) EnumSet<ProductStatus> statuses) {
16 
17 ProductFilter filter = ProductFilter.builder()
18 .tags(tags == null ? List.of() : tags)
19 .ids(ids)
20 .statuses(statuses == null ? EnumSet.allOf(ProductStatus.class) : statuses)
21 .build();
22 
23 return productQueryService.findMatching(filter).stream()
24 .map(ProductView::from)
25 .toList();
26 }
27}
01 / 01
STEP 01

Walkthrough

Space play ←→ step click any line
Three takeaways
  1. 1Spring can bind a query parameter straight into a List, Set, or EnumSet, converting each element to the target type automatically.
  2. 2Defaulting absent optional parameters at the controller boundary keeps downstream services free of null checks.
  3. 3Mapping entities to view objects before returning keeps the API contract decoupled from the persistence model.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
typescript
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import * as Joi from 'joi';
 

Validating env config at boot in NestJS

configuration schema-validation environment-variables
Intermediate 8 steps
java
@Component
@Converter
public class EncryptedStringConverter implements AttributeConverter<String, String> {
 

Transparent column encryption in Spring & JPA

encryption aes-gcm jpa-converter
Advanced 10 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
Intermediate 8 steps
java
package com.acme.billing.config;
 
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;

Feature-flagged beans with Spring @ConditionalOnProperty

feature-flags conditional-beans strategy-pattern
Intermediate 5 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps

Share this explainer

Here's the card — post it anywhere.

Binding collection query params in Spring — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code