This is the fast-food set up for Dynamic Search (aka. Advanced Search) for Spring Boot, built on top of Spring Data Jpa.
This provides typical settings for Criteria Search / Specification usages & validation supports.
Instead of boilerplate codes, what you need are:
Controller:
@RestController
@RequiredArgsConstructor
public class SearchController {
private final SearchService searchService;
@PostMapping("/search")
public ResponseEntity<Page<SearchEntity>> search(@RequestBody SearchCriteria criteria) {
return ResponseEntity.ok(searchService.search(criteria));
}
}
Service:
@Service
@RequiredArgsConstructor
public class SearchService {
private final EntityRepository entityRepository;
private final SpecBuilder<SearchEntity> builder = SpecBuilders.getInstance(SearchEntity.class);
public Page<SearchEntity> search(SearchCriteria searchCriteria) {
Specification<SearchEntity> spec = builder.build(searchCriteria.getCriteria());
return entityRepository.findAll(spec, searchCriteria.getPageable());
}
}
Repository:
@Repository
public interface EntityRepository extends JpaRepository<SearchEntity, Long>,
JpaSpecificationExecutor<SearchEntity> {
}
That's it! Everything is on your table.
Maven:
<dependency>
<groupId>io.github.nickngn</groupId>
<artifactId>spring-boot-starter-dynamic-search</artifactId>
<version>0.1.0</version>
</dependency>
Gradle:
implementation group: 'io.github.nickngn', name: 'spring-boot-starter-dynamic-search', version: '0.1.0'
- Generate SQL conditions in WHERE clause.