【问题标题】:Get parameters out from QueryDSL Predicate Object从 QueryDSL 谓词对象中获取参数
【发布时间】:2018-08-15 05:12:31
【问题描述】:

我使用带有 spring REST 端点的 QueryDSL 谓词对象来检索和查询参数值。

@GetMapping("/{subjectId}/students")
@RolesAllowed( {Roles.PLATFORM_ADMIN, Roles.USER})
public List<StudentResponse> getAllStudents(@PathVariable final String subjectId,                                
                                      @QuerydslPredicate(root = Student.class) final Predicate predicate) {     

        final Predicate searchPredicate = studentPredicate()
                .predicate(predicate)
                .subjectId(subjectId)
                .build();
        return studentService.findBySubjectId(subjectId, searchPredicate);

}

student Class 包含 studentIdstudentName 属性;

现在,如果有人调用 https://hostname/{subjectId}/students?studentId=1234&studentName=test

然后上面的代码生成带有参数值的谓词对象。但是我需要从谓词对象中获取超过 2 个参数值,以便除了数据库查询之外进行进一步处理。我没有看到任何从谓词对象中检索值的支持方法。那么我该怎么做呢?

【问题讨论】:

    标签: java spring rest predicate querydsl


    【解决方案1】:

    没有直接的方法可以做到这一点。
    但是你可以试试这个。

    predicate.toString(); ---> 这将打印 user.Studentid=1234 && user.studentName=test
    由此,您可以进行字符串拆分。

    另一种方法是使用

    predicate.getClass()); ----> 这将为您提供类名,例如

    classcom.querydsl.core.types.PredicateOperation(在多个查询条件的情况下)

    class com.querydsl.core.types.dsl.BooleanOperation(在单个查询条件的情况下)。

    有了这个,你可以将谓词类型转换为相应的类型,然后执行getArgs()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      相关资源
      最近更新 更多