java
51 lines · 7 steps
Binding paged search filters in Spring
A Spring REST endpoint binds query parameters into a validated criteria object and returns a paginated result.
Explained by
highlit
1@RestController
2@RequestMapping("/api/products")
3@RequiredArgsConstructor
4public class ProductSearchController {
5
6 private final ProductQueryService productQueryService;
7
8 @GetMapping("/search")
9 public Page<ProductSummary> search(@ModelAttribute @Valid ProductSearchCriteria criteria,
10 @PageableDefault(size = 20, sort = "createdAt", direction = Sort.Direction.DESC)
11 Pageable pageable) {
12 return productQueryService.search(criteria, pageable);
13 }
14
15 @Getter
16 @Setter
17 public static class ProductSearchCriteria {
18
19 private String keyword;
20
21 private Set<Long> categoryIds = new HashSet<>();
22
23 @Valid
24 private PriceRange price = new PriceRange();
25
26 @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
27 private LocalDate releasedAfter;
28
29 private boolean inStockOnly;
30
31 public boolean hasKeyword() {
32 return StringUtils.hasText(keyword);
33 }
34 }
35
36 @Getter
37 @Setter
38 public static class PriceRange {
39
40 @PositiveOrZero
41 private BigDecimal min;
42
43 @Positive
44 private BigDecimal max;
45
46 @AssertTrue(message = "price.max must be greater than or equal to price.min")
47 public boolean isRangeValid() {
48 return min == null || max == null || max.compareTo(min) >= 0;
49 }
50 }
51}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1@ModelAttribute binds flat query parameters into a nested object graph, so complex filters stay strongly typed.
- 2@Valid with bean-validation constraints lets you reject bad requests declaratively, including cross-field checks via @AssertTrue.
- 3Accepting a Pageable with @PageableDefault gives clients paging and sorting control while guaranteeing safe defaults.
Related explainers
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 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
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
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
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/binding-paged-search-filters-in-spring-explained-java-67da/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.