Spring Data Rest - Parameters with default values

此生再无相见时 提交于 2020-01-13 09:59:10

问题


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.


回答1:


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);



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/25761416/spring-data-rest-parameters-with-default-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!