Search code examples
javaspringrestpredicatequerydsl

Get parameters out from QueryDSL Predicate Object


I use QueryDSL predicate object with a spring REST endpoint to retrieve and query param values.

@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 contains studentId and studentName attributes;

Now, if someone invokes https://hostname/{subjectId}/students?studentId=1234&studentName=test

Then above code generates the predicate object with param values. But I need to get above 2 param values out from predicate object for further processing apart from the db querying. I don't see any supportive method from predicate object to retrieve values out. So how can I do it?


Solution

  • There is no straightforward way to do this.
    However You can try this.

    predicate.toString(); ---> This would print user.Studentid=1234 && user.studentName=test
    From this, you can do a string split.

    Another way is with

    predicate.getClass()); ----> This will give you the class name like

    class com.querydsl.core.types.PredicateOperation (in case of more than one query conditions)

    class com.querydsl.core.types.dsl.BooleanOperation (in case of single query condition).

    With this you could typecast predicate to corresponding type and then do getArgs().