Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document QueryDSL endpoint with Swagger

I'm using Spring Boot Data, QueryDSL and Swagger. I've define endpoint like this:

@GetMapping
public ResponseEntity<?> listOfThings(
        @PageableDefault(size = 20, sort = "uID", direction = Sort.Direction.DESC) final Pageable pageable,
        @QuerydslPredicate(root = Thing.class) final Predicate predicate)

However Swagger define only variables: page, size, sort - it doesn't seem to parse Entity to show all fields as filterable.

I have repository like this:

@Repository
public interface ThingRepository
        extends JpaSpecificationExecutor<Thing>, CrudRepository<Thing, String>, PagingAndSortingRepository<Thing, String>,
        QuerydslPredicateExecutor<Thing>, QuerydslBinderCustomizer<QThing>
{
    @Override
    default void customize(final QuerydslBindings bindings, final QThing thing)
    {
        bindings.bind(thing.status).first((status, value) -> status.eq(value));
        bindings.bind(thing.recipient).first(StringExpression::containsIgnoreCase);
        bindings.bind(String.class).first((StringPath path, String value) -> path.containsIgnoreCase(value));
    }

}

I expect Swagger to display all String fields as filters, especially status & recipient which are strictly defined.

like image 488
Marx Avatar asked Aug 12 '26 19:08

Marx


1 Answers

Use maven dependency:

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>1.6.8</version>
    </dependency>

And specify your endpoint like this:

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Case>> getCasesByQuery(@ParameterObject
                                                  @QuerydslPredicate(root = Case.class, bindings = CaseRepository.class) Predicate predicate,
                                                  @ParameterObject Pageable pageable) {

Now you should see all query params in SwaggerUI.

like image 156
Dennis Avatar answered Aug 14 '26 08:08

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!