Categories
java spring spring-data spring-data-rest

Spring Data Query Method with Optional @Param

Is it possible to allow query method @Params to be optional, specifically in the case of Spring Data REST?

For example, I’d like to bind a very similar search to the same resource path. To do this now, I would need something like the following:

@RestResource(path = "driver", rel = "byDriver")
List<Bar> findByDriverId(@Param("id") String id, Pageable pageable);
@RestResource(path = "driverAndSpan", rel = "byDriverAndSpan")
List<Bar> findByDriverIdAndStartTimeGreaterThanEqualAndEndTimeLessThanEqual(@Param("id") String id, @Param("start") Date start,
@Param("end") Date end, Pageable pageable);

Which gives me:

byDriver: {
href: "http://localhost:8080/foo/search/driver{?id,page,size,sort}",
},
byDriverAndSpan: {
href: "http://localhost:8080/foo/search/driverAndSpan{?id,start,end,page,size,sort}",
}

What I want is to be able to see something like the following path, where start and end are optional parameters, rather than defining multiple methods in my Repository.

byDriverAndSpan: {
href: "http://localhost:8080/foo/search/driverAndSpan{?id,*start,*end,page,size,sort}",
}

Which could potentially look like:

@RestResource(path = "driverAndSpan", rel = "byDriverAndSpan")
List<Bar> findByDriverIdAndStartTimeGreaterThanEqualAndEndTimeLessThanEqual(@Param("id") String id, @Param(value = "start", optional = true) Date start,
@Param(value = "end", optional = true) Date end, Pageable pageable);