java – 从QueryDSL谓词对象中获取参数

我使用带有
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类包含studentId和studentName属性;

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

然后上面的代码生成带有param值的谓词对象.但是我需要从谓词对象中获得超过2个参数值,以便进行进一步处理,而不是db查询.我没有从谓词对象中看到任何支持方法来检索值.那我该怎么办呢?

最佳答案 没有直接的方法来做到这一点.

但是你可以尝试这个.

predicate.toString(); —>这将打印user.Studentid = 1234&& user.studentName =测试
从这里,您可以进行字符串拆分.

另一种方式是

predicate.getClass()); —->这将为您提供类名称

class com.querydsl.core.types.PredicateOperation(如果有多个查询条件)

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

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

点赞