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
java
public class SlidingLogRateLimiter { private final int maxRequests; private final long windowMillis;
How a sliding-log rate limiter works
rate-limiting
concurrency
sliding-window
Advanced
8 steps
php
<?php namespace App\Http\Controllers;
Streaming a filtered CSV export in Laravel
streaming
csv-export
query-builder
Intermediate
9 steps
java
public final class Slugifier { private static final Pattern NON_LATIN = Pattern.compile("[^\\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
Building a URL slugifier in Java
regex
unicode-normalization
string-processing
Intermediate
8 steps
java
@Repository public interface OrderRepository extends JpaRepository<Order, Long> { @Query("""
Keyset pagination with Spring Data JPA
pagination
cursor
jpa
Advanced
8 steps
java
public record FixedWidthField(String name, int start, int end) { public String extract(String line) { int from = Math.min(start, line.length());
Parsing fixed-width text in Java
records
parsing
streams
Intermediate
7 steps
java
@RestController @RequestMapping("/api/quotes") public class QuoteStreamController {
Streaming market quotes with Spring WebFlux
reactive-streams
backpressure
server-sent-events
Advanced
9 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.