如何使用Spring Data REST进行高级搜索? [英] How to make an advanced search with Spring Data REST?

查看:175
本文介绍了如何使用Spring Data REST进行高级搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是使用Spring Data REST进行高级搜索。
如何实现它?

My task is to make an advanced search with Spring Data REST. How can I implement it?

我设法做了一个简单的搜索方法,如下所示:

I managed to make a method to do a simple search, like this one:

public interface ExampleRepository extends CrudRepository<Example, UUID>{

    @RestResource(path="searchByName", rel="searchByName")
    Example findByExampleName(@Param("example") String exampleName);

}

如果我必须简单地去url:

This example works perfectly if I have to go simply to the url:

.../api/examples/search/searchByName?example=myExample

但如果要搜索多个字段,我该怎么做?

But what I have to do if there are more than one field to search?

例如,如果我的Example类有5个字段,那么我应该使用所有possibiles文件进行高级搜索?

For example, if my Example class has 5 fields, what implementation should I have to make an advanced search with all possibiles fileds?

考虑这个:

.../api/examples/search/searchByName?filed1=value1&field2=value2&field4=value4

这一个:

.../api/examples/search/searchByName?filed1=value1&field3=value3

我是什么必须以适当的方式实现此搜索吗?

What I have to do to implement this search in appropriate way?

谢谢。

推荐答案

Spring Data Rest已将QueryDSL与Web支持集成在一起,您可以使用它为您的高级搜索要求。您需要更改您的存储库以实现 QueryDslPredicateExecutor 并且开箱即用。

Spring Data Rest has integrated QueryDSL with web support as well which you can use for your advanced search requirement. You need to change your repository to implement QueryDslPredicateExecutor and things will work out of the box.

这是一个示例来自博客有关该功能的文章:

Here is a sample from the blog article about the feature:

$ http :8080/api/stores?address.city=York    
{
    "_embedded": {
        "stores": [
            {
                "_links": {
                    …
                }, 
                "address": {
                    "city": "New York", 
                    "location": { "x": -73.938421, "y": 40.851 }, 
                    "street": "803 W 181st St", 
                    "zip": "10033-4516"
                }, 
                "name": "Washington Hgts/181st St"
            }, 
            {
                "_links": {
                    …
                }, 
                "address": {
                    "city": "New York", 
                    "location": { "x": -73.939822, "y": 40.84135 }, 
                    "street": "4001 Broadway", 
                    "zip": "10032-1508"
                }, 
                "name": "168th & Broadway"
            }, 
            …
        ]
    }, 
    "_links": {
        …
    }, 
    "page": {
        "number": 0, 
        "size": 20, 
        "totalElements": 209, 
        "totalPages": 11
    }
}

这篇关于如何使用Spring Data REST进行高级搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆