Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 396
ChrisGeo Avatar asked Sep 10 '14 09:09

ChrisGeo


People also ask

How do I set default value in RequestParam?

By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting @RequestParam 's required attribute to false . In the above example, the since query param is optional: @RequestParam(value="since", required=false) ).

Why is Spring Data REST not recommended in real world applications?

It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.

What does the @RepositoryRestResource annotation do?

The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint. If we decided to omit it, Spring would automatically create an endpoint at “/websiteUsers” instead of “/users“. That's it! We now have a fully-functional REST API.


2 Answers

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);
like image 104
Shempus Avatar answered Nov 04 '22 00:11

Shempus


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

like image 45
Dominik Avatar answered Nov 04 '22 00:11

Dominik