Skip to content

nickngn/dynamic-search-spring-boot-starter

Repository files navigation

Spring Boot Starter Dynamic Search

License javadoc

What is this?

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.

Installation:

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'

Validate syntax for key/values using Jakarta/Javax annotations

  • Generate SQL conditions in WHERE clause.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages