Repository navigation
4.0.0
CustomSpecificationSearch
With CustomSpecificationSearch, you can execute specifications using a request parameter. Follow the steps below to use it:
1. Create a Custom Specification
Define a custom specification for your model. For example, to create a specification that checks if a product is in one of two specific states:
public static Specification<Product> readyToUse() {
return (root, criteriaQuery, criteriaBuilder) -> {
return criteriaBuilder.or(
criteriaBuilder.equal(root.get(Product_.status), ENUM_STATUS_1),
criteriaBuilder.equal(root.get(Product_.status), ENUM_STATUS_2)
);
};
}2. Add a @Configuration Class to Your Project
Create a class annotated with @Configuration to register the custom specification in CustomSpecificationSearch:
@Configuration
class MonkeySpecificationSearchConfiguration {
@PostConstruct
void customSpecification() {
CustomSpecificationSearch.getInstance().add("productReadyToUse", ProductSpecification.readyToUse());
}
}3. Call the API Using the Custom Predicate
You can now call your API using the customPredicate as a search parameter. For example:
GET: host/v2/products?search=customPredicate:readyToUse