ChrisGeo
ChrisGeo

Reputation: 3907

Spring Data Rest - Parameters with default values

I have created the following @RepositoryRestResource query where I want to create a dynamic query for my rest api. So basically I would want to do something like:

myHost/myApp/data/search/all?name=me&age=20&address=myhome&etc=etc

So I have created the query below:

   @Query("Select t from Data t " +
            "where " +
            "t.name like :name AND " +
            "t.age = :age AND " +
            "t.address = :address AND " +
            "t.etc= :etc"
    @RestResource(path = "all", rel = "all")
    Page findByAll(@Param("name") String name, @Param("age") String age,
            @Param("address") String address, @Param("etc") String etc, Page page);

Obviously some of these may not have been entered. Is there some way to define default values on the Repository? So for example I would want name to have a default value of %.

I'm not entirely sure this approach is the correct one for what I want to do, so any alternative suggestions are welcome.

Upvotes: 6

Views: 10362

Answers (3)

Dominik
Dominik

Reputation: 2906

So, one possible solution might be that you might go to the controller and use in your @Controller/@RestController your @RequestParam with attributes required = falseand defaultValue = "%".

A corresponding call might look like this:

@RestController
...
@RequestMapping(value = "[myCallFromFrontend]", method = RequestMethod.GET)
public ResponseItem getItemsByFilters (
    @RequestParam(required = false, defaultValue = "%") String name,
    @RequestParam(required = false, defaultValue = "%") String age,
    @RequestParam(required = false, defaultValue = "%") String address,
    @RequestParam(required = false, defaultValue = "%") String etc,
    HttpServletResponse response){

    ResponseItem item = null;
    try {
         //you might do this in service layer.
         item = myRepository.findByAll(name, age, address, etc);
    } catch (myException mE) {
            log.error("...", mE);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        return item;
    }

So now you got your default values set. I don't know a way to set them directly at repository-level, though. I created a question about that right here

Upvotes: 2

Shempus
Shempus

Reputation: 116

I know this is older, but I had a similar issue and solved it as follows:

@Query("Select t from Data t " +
        "where " +
        "(:name IS NULL or t.name like :name) AND " +
        "(:age IS NULL or t.age = :age) AND " +
        "(:address IS NULL or t.address = :address) AND " +
        "(:etc IS NULL or t.etc= :etc)")
@RestResource(path = "all", rel = "all")
Page findByAll(@Param("name") String name, @Param("age") String age,
        @Param("address") String address, @Param("etc") String etc, Page page);

Upvotes: 8

Andres
Andres

Reputation: 10717

Faced the same problem, and as far as I could research, the best way for solving this is to make a different query for each set of values.

Upvotes: 1

Related Questions