java 如何使用 Spring 以安静的方式过滤数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42087885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:21:30  来源:igfitidea点击:

How do I filter data in a restful way using Spring?

javaspringhibernaterestfilter

提问by Dawid

As the title says.

正如标题所说。

I basically would love to do requests like

我基本上很乐意做这样的请求

/api/todos/?completed=eq.true&created_at=lt.1486462109399

Is there any ready spring wayof achieving such? Something akin to the Page/Pageable mechanism would be great.

有没有准备好spring way实现这样的?类似于 Page/Pageable 机制的东西会很棒。

If there is none I think I could implement it using Hibernate Criteria Queries & Argument Re-solvers. Basically allowing me to write my controllers like

如果没有,我想我可以使用 Hibernate Criteria Queries & Argument Re-solvers 来实现它。基本上允许我写我的控制器

 @GetMapping
 public ResponseEntity<Page<TodoDTO>> listAll(Criteria criteria, Pageable pageable) 
 {
        Page<Todo> todos = todoService.listAll(criteria, pageable)
        ...
 }

A custom Argument resolver would be responsible for turning the query string into a Criteria. Not quite sure yet how I would handle it within the service but that's the direction in which I would try to implement this.

自定义参数解析器将负责将查询字符串转换为条件。还不太确定我将如何在服务中处理它,但这是我将尝试实施的方向。

Would that be a good approach? Any recommendations? (All assuming there are no ready mechanism for such already).

这会是一个好方法吗?有什么建议吗?(所有这些都假设已经没有现成的机制)。

Your help is much appreciated.

非常感谢您的帮助。

采纳答案by naXa

Another option to build a fluent query API is to use a RSQL parser. RSQLis a query language for parametrized filtering of entries in RESTful APIs. Follow this articleand your API would be able to handle URLs like:

构建流畅查询 API 的另一种选择是使用 RSQL 解析器。RSQL是一种查询语言,用于对 RESTful API 中的条目进行参数化过滤。按照这篇文章,您的 API 将能够处理如下 URL:

http://localhost:8080/users?search=firstName==jo*;age<25

Sample controller:

示例控制器:

@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Autowired
    private UserRepository repo;

    @GetMapping
    public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
        Node rootNode = new RSQLParser().parse(search);
        Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
        return repo.findAll(spec);
    }

}

回答by naXa

You can build a Search/Filter REST API using Spring Data JPA and Specifications. Here is a test URL example that the resulting API would be able to handle:

您可以使用 Spring Data JPA 和 Specifications构建搜索/过滤器 REST API。这是生成的 API 能够处理的测试 URL 示例:

http://localhost:8080/users?search=lastName:doe,age>25

and example controller:

和示例控制器:

@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Autowired
    private UserRepository repo;

    @GetMapping
    public List<User> search(@RequestParam(value = "search") String search) {
        UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
        Pattern pattern = Pattern.compile("(\w+?)(:|<|>)(\w+?),");
        Matcher matcher = pattern.matcher(search + ",");
        while (matcher.find()) {
            builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
        }

        Specification<User> spec = builder.build();
        return repo.findAll(spec);
    }
}