Spring Rest Advance Search Generator

Description

Di tutorial ini saya akan sharing cara cepat buat api search filter dengan bantuan liblary spring-filter, spring-filter ini akan membuatkan sebuah Spesification yang nantinya kita gunakan untuk filter di repositorynya, untuk filter field nya dia akan mengikuti properti entitynya.


Feature Spring-Filter

  1. Menggunakan operator sama dengan:
    Request : /cars?search=color:Red
    equal operator example

  2. Mengunakan operator tidak sama dengan!
    Request : /cars?search=color!Red not equal operator example

  3. Menggunakan operator lebih besar dari >
    Request : /cars?search=creationyear>2017
    greater than operator example

  4. Menggunakan operator kurang dari <
    Request : /cars?search=price<100000
    less than operator example

  5. Menggunakan operator like *
    Request : /cars?search=brand:Aston*
    starts with operator example

  6. Menggunakan operator OR
    Request : /cars?search=color:Red OR color:Blue
    or operator example

  7. Menggunakan operator AND
    Request : /cars?search=brand:Aston* AND price<300000
    and operator example

  8. Menggunakan kurung buka ( )
    Request : /cars?search=( brand:Nissan OR brand:Chevrolet ) AND color:Blue
    Note: Spaces inside the parenthesis are not necessary parenthesis example

  9. Menggunakan kutip ' '
    Request : /cars?search=model:'Spacetourer Business Lounge'
    space example

  10. Menggunakan spesial karakter
    Request: /cars?search=model:中华V7 special characters example

  11. Menyelam ke child propertie lebih dalam
    Request : /cars?search=options.transmission:Auto deep field example

  12. Complex example
    Request : /cars?search=creationyear:2018 AND price<300000 AND (color:Yellow OR color:Blue) AND options.transmission:Auto complex example

Install

Maven

Add the repo to your project inside your pom.xml file

<dependency>
    <groupId>com.sipios</groupId>
    <artifactId>spring-search</artifactId>
    <version>0.2.0</version>
</dependency>

Gradle

Add the repo to your project by adding implementation 'com.sipios:spring-search:0.2.0' in your build.gradle file.


Usage

Repository

import com.example.springbootadvancesearch.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface CustomerRepository extends JpaRepository<Customer, Integer>, JpaSpecificationExecutor<Customer> {
}

Controller

Sebelumn menggunakan import terlebih dahulu @SearchSpec

import com.sipios.springsearch.anotation.SearchSpec

Menggunakan Spring-filter

@GetMapping("/search")
    public ResponseEntity search(@SearchSpec Specification<Customer> search){
        return ResponseEntity.ok(customerRepository.findAll(Specification.where(search)));
    }

Test






Komentar

Postingan populer dari blog ini

whois

Membuat export dan import Excel di spring boot

Spring Boot CRUD Thymeleaf-Pagination + Bootstrap Dynamic Modals