javarestspring-mvcswaggerspringdoc

Grouping multiple request parameters as single param for @RestController method


Suppose my Swagger-enabled endpoint receives lots of request parameters. I don't want to list all of them one by one. Instead, I want to group them somehow. Preferably, as a DTO: if I declare a Map, allowed attribute names won't be displayed. I hoped this would work:

    @GetMapping
    public ResponseEntity<List<UserResponseDto>> findUsers(@RequestParam FindUserRequestDto parameterMap) {
        // method body
    }
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Getter
@Setter
public class FindUserRequestDto {

    private LocalDate dateOfBirth;
    private String phone;
    private String name;
    private String email;
    private int pageNumber;
    private int pageSize;
}

However, it does not. In the Swagger UI, the endpoint is displayed like this (as if it expects a literal object JSON):

visual representation of my DTO-accepting GET endpoint in Swagger UI

Declaring the parameter as a Map

    @GetMapping
    public ResponseEntity<List<UserResponseDto>> findUsers(@RequestParam Map<String, String> parameterMap) {
        // method body
    }

has a similar effect:

visual representation of my Map-accepting GET endpoint in Swagger UI

Is there a way to group multiple request parameters together so that a @RestController method accepts them as one parameter?

Note GET endpoints are not supposed to receive a body.

Spring Boot 3.4.5, Spring Doc 2.8.8.

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.8.8</version>
        </dependency>

Solution

  • @ParameterObject is the answer:

        import org.springdoc.core.annotations.ParameterObject;
        
        // ...    
    
        @GetMapping
        public ResponseEntity<List<UserResponseDto>> findUsers(@ParameterObject FindUserRequestDto userRequestDto) {
            // ...
        }