Skip to content

Instantly share code, notes, and snippets.

@Xanaxiel
Forked from odrotbohm/StoreRepository.java
Created October 22, 2021 09:08
Show Gist options
  • Save Xanaxiel/5c0022b01ed8cf668385caec967a2ec9 to your computer and use it in GitHub Desktop.
Save Xanaxiel/5c0022b01ed8cf668385caec967a2ec9 to your computer and use it in GitHub Desktop.

Revisions

  1. @odrotbohm odrotbohm revised this gist Jul 15, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion readme.adoc
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,6 @@ GET /stores?name=Foo

    will return all stores with a name of "Foo". A plain `equals(…)` comparison might be a bit strict, so we expose a `QuerydslBinderCustomizer` interface that will allow you to customize the way properties are bound within the overall predicate.

    As you can see above, the easiest way to define customized bindings is by implementing `customize(…)` in a default method and use the provided `QuerydslBindings` and entity path to tweak the binding as you like. You see we define the city to be bound via `endsWith(…)`, `String` properties in general are bound via a `contains(…)` to make the resulting predicate less restrictive.
    As you can see above, the easiest way to define customized bindings is by implementing `customize(…)` in a default method and use the provided `QuerydslBindings` and entity path to tweak the binding as you like. You see we define the city to be bound via `endsWith(…)`, `String` properties in general are bound via a `contains(…)` to make the resulting predicate less restrictive.

    A working example can be found in https://github.com/spring-projects/spring-data-examples/blob/querydsl-binding/rest/starbucks/src/main/java/example/stores/StoreRepository.java[this feature branch] of the Spring Data Examples repository.
  2. @odrotbohm odrotbohm revised this gist Jul 15, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions StoreRepository.java
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    public interface StoreRepository extends PagingAndSortingRepository<Store, String>, QueryDslPredicateExecutor<Store>,
    QuerydslBinderCustomizer<QStore> {
    public interface StoreRepository extends PagingAndSortingRepository<Store, String>,
    QueryDslPredicateExecutor<Store>, QuerydslBinderCustomizer<QStore> {

    @RestResource(rel = "by-location")
    Page<Store> findByAddressLocationNear(Point location, Distance distance, Pageable pageable);

    default void customize(QuerydslBindings bindings, QStore store) {
    bindings.bind(store.address.city).single((path, value) -> path.startsWith(value));
    bindings.bind(store.address.city).single((path, value) -> path.startsWith(value));
    bindings.bind(String.class).single((StringPath path, String value) -> path.contains(value));
    }
    }
  3. @odrotbohm odrotbohm created this gist Jul 15, 2015.
    11 changes: 11 additions & 0 deletions StoreRepository.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    public interface StoreRepository extends PagingAndSortingRepository<Store, String>, QueryDslPredicateExecutor<Store>,
    QuerydslBinderCustomizer<QStore> {

    @RestResource(rel = "by-location")
    Page<Store> findByAddressLocationNear(Point location, Distance distance, Pageable pageable);

    default void customize(QuerydslBindings bindings, QStore store) {
    bindings.bind(store.address.city).single((path, value) -> path.startsWith(value));
    bindings.bind(String.class).single((StringPath path, String value) -> path.contains(value));
    }
    }
    11 changes: 11 additions & 0 deletions readme.adoc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    We're just experimenting with some Spring Data / Spring MVC integration that will allow you to bind property path expressions in the request to a Querydsl `Predicate` as a Spring MVC controller method argument. This is cool for manually implemented controllers so I wondered what it takes to integrate that with Spring Data REST.

    In the example above you see `StoreRepository` extending `QuerydslPredicateExecutor<Store>`. In the coming version of Spring Data REST, this will cause the collection resource exposed for the repository to accept property based filtering:

    ```
    GET /stores?name=Foo
    ```

    will return all stores with a name of "Foo". A plain `equals(…)` comparison might be a bit strict, so we expose a `QuerydslBinderCustomizer` interface that will allow you to customize the way properties are bound within the overall predicate.

    As you can see above, the easiest way to define customized bindings is by implementing `customize(…)` in a default method and use the provided `QuerydslBindings` and entity path to tweak the binding as you like. You see we define the city to be bound via `endsWith(…)`, `String` properties in general are bound via a `contains(…)` to make the resulting predicate less restrictive.