CDT
CDT

Reputation: 10621

Spring Data Rest - Configure findBy path

With the following Repository:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PeopleRepository extends PagingAndSortingRepository<People, String> {

    @RestResource
    List<People> findByName(@Param("name") String name);

}

URL for findByName is automatically set to /people/search/findByName. But it seems quite verbose, can the URL be configured to /people and query is like /people?name=john ?

Upvotes: 1

Views: 957

Answers (2)

Cepr0
Cepr0

Reputation: 30309

You can change the last part of the path (see the doc):

@RestResource(path = "byName", rel = "byName")
List<People> findByName(@Param("name") String name);
/people/search/byName?name=john

For comprehensive implementation with QueryDsl see @AlanHay answer (more info is here)

Upvotes: 0

Alan Hay
Alan Hay

Reputation: 23226

If you use the QueryDSL extension you can have the query in that format and have the additional benefit of being able to filter by any combination of properties without having to write any query methods:

Configuration (Assuming Maven)

<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-apt</artifactId> 
    <version>4.1.4</version>
    </dependency>
<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-jpa</artifactId> 
    <version>4.1.4</version> 
</dependency>

Then simply update your repository definition:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PeopleRepository extends PagingAndSortingRepository<People, String>, 
                                               QuerydslPredicateExecutor<People> {


}

You should now be able to filter like:

/people?name=john
/people?name=John&address.town=London 

etc

Upvotes: 2

Related Questions