0

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>
1
  • @TevinS what about @ParameterObject? Commented May 24 at 12:28

1 Answer 1

0

@ParameterObject is the answer:

    import org.springdoc.core.annotations.ParameterObject;
    
    // ...    

    @GetMapping
    public ResponseEntity<List<UserResponseDto>> findUsers(@ParameterObject FindUserRequestDto userRequestDto) {
        // ...
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.