【问题标题】:Split and Validate Comparison Expression in java在java中拆分和验证比较表达式
【发布时间】:2018-09-23 09:27:00
【问题描述】:

我正在构建一个 Rest 服务。我的服务资源方法是这样的:

public Response<List<AuditEvent>> getAuditEvents(@QueryParam("filters") List<AuditRequest.Filter> filters,
                                                 @QueryParam("orderBy") @DefaultValue("id") String order,
                                                 @QueryParam("isDesc") @DefaultValue("true") boolean desc,
                                                 @QueryParam("startTime") long startTimestamp,
                                                 @QueryParam("endTime") long endTimestamp,
                                                 @QueryParam("limit") @DefaultValue("20") int size,
                                                 @QueryParam("offset") @DefaultValue("0") int startIndex)

这里的过滤器可以是多个表达式,例如:

/api/audit/query/?filters=username='admin'&filters=customerId=1&limit=1&orderBy=rowId&isDesc=false&offset=2

我也做了一个过滤器类:

public static class Filter {
    private String propertyId;
    private String op;
    private Object value;

}

请给我一个很好的方法来将 Resource 方法中的 List&lt;String&gt; 转换为 List&lt;Filters&gt; 并验证每个字符串表达式是否有效。基本上,我需要一个解析器来将此字符串表达式转换为我的过滤器对象,其中运算符可以是任何有效的运算符。

【问题讨论】:

    标签: java json rest jax-rs


    【解决方案1】:

    我认为您的查询不够清晰。

    请考虑:

    https://github.com/jirutka/rsql-parser

    几个例子:

    • name=="杀死比尔";year=gt=2003
    • name=="Kill Bill" 和年份>2003
    • genres=in=(sci-fi,action);(director=='Christopher Nolan',actor==*Bale);year=ge=2000
    • genres=in=(sci-fi,action) and (director=='Christopher Nolan' or actor==*Bale) and year>=2000
    • director.lastName==Nolan;year=ge=2000;year=lt=2010

    如果您想编写自己的解决方案,请考虑矩阵变量: 过滤器=name=abc;age>40;....

    希望它有效。

    【讨论】:

    • 我没有说这是错的。只是不清楚和精简(这里的过滤器参数太多)
    • 好的明白了你的意思......但是如果我想把它作为 queryparam 中的一个列表,还有什么办法。因为如果我使用这种方式 filters=name=abc;age>40;我必须按分隔符转换为列表。
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多