Yusuf
1 min readFeb 21, 2021

--

Spring Filter

😕 You surely know how painful it can be to implement something like

/cars?filter= brand : ‘audi’

But you can actually write powerful queries with Spring Filter 🚀

/cars?filter= average(ratings) > 4.5 and brand.name in [‘audi’, ‘land rover’] and (year > 2018 or km < 50000) and color : ‘white’ and accidents is empty

And that for any entity, any field! You have enums, dates, booleans, relations which require joins? No problem! All of that is filterable 😎

And it’s simple as that

@GetMapping(value = "/cars")
public List<Car> search(@Filter Specification<Car> spec) {
return carRepository.findAll(spec);
}

🔥 Oh, and don’t forget the dependency

<dependency>
<groupId>com.turkraft</groupId>
<artifactId>spring-filter</artifactId>
<version>3.0.8</version>
</dependency>

You want to build queries in your client app? Here you go 👷

@Autowired FilterBuilder fb;
FilterNode filter = fb.field("year").equal(fb.input(2023)).and(fb.isNull(fb.field("category"))).get();

@Autowired ConversionService cs;
String query = cs.convert(filter, String.class); // year : 2023 and category is null

Want to try the whole thing live? Come on!

You may find more examples, different usages, and the full query syntax in the GitHub repository at turkraft/spring-filter⭐

--

--

Yusuf

Software Engineer. Working with Java/Spring and PHP/Laravel. Building APIs and SaaSes. Occasionally playing with matrices.