data-search-api

A library that helps you instantly turn your Spring powered endpoints into a query engine. It makes use of AOP to intercept the calls to your Controller or RestController endpoints and then builds a Specification from the provided query parameters

License

License

Categories

Categories

Data Search Business Logic Libraries
GroupId

GroupId

com.github.magrifle
ArtifactId

ArtifactId

data-search-api
Last Version

Last Version

2.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

data-search-api
A library that helps you instantly turn your Spring powered endpoints into a query engine. It makes use of AOP to intercept the calls to your Controller or RestController endpoints and then builds a Specification from the provided query parameters
Project URL

Project URL

https://github.com/magrifle/search-api
Source Code Management

Source Code Management

https://github.com/magrifle/search-api.git

Download data-search-api

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.magrifle/data-search-api/ -->
<dependency>
    <groupId>com.github.magrifle</groupId>
    <artifactId>data-search-api</artifactId>
    <version>2.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.magrifle/data-search-api/
implementation 'com.github.magrifle:data-search-api:2.0.5'
// https://jarcasting.com/artifacts/com.github.magrifle/data-search-api/
implementation ("com.github.magrifle:data-search-api:2.0.5")
'com.github.magrifle:data-search-api:jar:2.0.5'
<dependency org="com.github.magrifle" name="data-search-api" rev="2.0.5">
  <artifact name="data-search-api" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.magrifle', module='data-search-api', version='2.0.5')
)
libraryDependencies += "com.github.magrifle" % "data-search-api" % "2.0.5"
[com.github.magrifle/data-search-api "2.0.5"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.guava : guava jar 19.0

provided (4)

Group / Artifact Type Version
org.hibernate.javax.persistence : hibernate-jpa-2.1-api jar 1.0.2.Final
org.springframework : spring-webmvc jar 5.2.6.RELEASE
javax.servlet : javax.servlet-api jar 4.0.1
org.springframework.data : spring-data-jpa jar 2.2.4.RELEASE

test (15)

Group / Artifact Type Version
org.hibernate : hibernate-core jar 5.3.7.Final
org.hibernate : hibernate-entitymanager jar 5.3.7.Final
junit : junit jar 4.13.1
org.mockito : mockito-core jar 2.23.0
org.springframework : spring-test jar 5.2.6.RELEASE
com.h2database : h2 jar 1.4.197
org.slf4j : slf4j-log4j12 jar 1.7.25
commons-dbcp : commons-dbcp jar 1.2.2
org.springframework : spring-jdbc jar 5.2.6.RELEASE
org.springframework : spring-aspects jar 5.2.6.RELEASE
com.fasterxml.jackson.core : jackson-core jar 2.9.7
com.fasterxml.jackson.core : jackson-databind jar 2.10.0.pr1
org.hamcrest : hamcrest-all jar 1.3
com.jayway.jsonpath : json-path jar 2.4.0
org.projectlombok : lombok jar 1.18.2

Project Modules

There are no modules declared in this project.

search-api

PRs Welcome Build Status Maven Central

A library that helps you instantly turn your Spring powered endpoints into a query engine. It makes use of AOP to intercept the calls to your @Controller or @RestController endpoint and then builds a Specification from the provided query parameters

Inspired by GitHub Search API

version spring-data compatibility
2.x > 2.x >
<2.x < 2.x

What's new - 2.0.5?

Allow support for @Discriminators when fetching entities

@Entity
@DiscriminatorValue("CAR")
public class CarEntity extends VehicleEntity
{
    private Integer numberOfDoors;
}


@Entity(name = "vehicle_entity")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "vehicle_type", discriminatorType = DiscriminatorType.STRING)
public class VehicleEntity
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Integer numberOfWheels;

}

//bean config
...
SearchKey numberOfDoors = new SearchKey("numberOfDoors", "vehicleEntity.numberOfDoors");
numberOfDoors.setType(CarEntity.class);
searchKeys.add(numberOfDoors);

Full Change log

Example

curl http://your.awesome.api/search?q=firstName:Jones,lastName:Fran*,dateCreated>2018-01-01,age<67,city:*ondon*

Configuration

  1. Add the dependency to the pom.xml file of your Spring boot or web MVC project. (Assume of course you're using maven package manager)
<dependency>
  <groupId>com.github.magrifle</groupId>
  <artifactId>data-search-api</artifactId>
</dependency>
  1. Next, you need to define a @Bean to enable the search API functionality
@Configuration
public class ApiSearchConfig {
    
    @Bean
    public DataSearchApi dataSearchApi() {
        return new DataSearchApi();
    }
}
  1. Next, we add a @Bean of type SearchConfigurer that holds the configuration of your data mapping. This bean has a method getSearchKeys() that contains a list of all the SearchKey that are allowed in your search API.

It also contains some the configuration methods like getDateKeyFormat() that can be used to specify the allowed date format in the query string of the search API.

@Configuration
public class ApiSearchConfig {

    ...

    @Bean
    public SearchConfigurer<Item> getSearchKeysForItem() {
        return new SearchConfigurer<Item>() {
            @Override
            public List<SearchKey> getSearchKeys() {
                List<SearchKey> searchKeys = new ArrayList();
                searchKeys.add(new SearchKey("name"));
                searchKeys.add(new SearchKey("pno", "passportNumber"));
                return searchKeys;
            }
        };
    }
}
  1. Finally, in your @Controller or @RestController, add the @SearchApi annotation
@RestController
public class ApiController {

    @Autowired
    private ItemRepository itemRepository;
    ...

    @SearchApi(entity = Item.class)
    @GetMapping("/search")
    public Page<Item> searchItems(SearchBuilder<Item> builder, Pageable pageable){
        return itemRepository.findAll(builder.build(), pageable);
    }
}

Note that your repository class must extend the JpaSpecificationExecutor<T> interface in spring-data to have access to the findAll(Specification) method.

Parameters

@SearchApi

Name Type Description
queryString String default "q". This is the query string parameter in the request that contains the search criteria.
keySeparator char default ",". The character used to separate different criteria in the queryString
entity class required. The entity class to be queried.
failOnMissingQueryString boolean default "false". By default, if the queryString is empty, the endpoint would query the repository with an empty criteria which translates to select * ... in sql. You can turn off this behaviour by setting this parameter to true in which case a SearchKeyValidationException exception is thrown if the queryString is missing or does not contain any criteria.

Versions

Version
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
1.0.6
1.0.5