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):
Declaring the parameter as a Map
@GetMapping
public ResponseEntity<List<UserResponseDto>> findUsers(@RequestParam Map<String, String> parameterMap) {
// method body
}
has a similar effect:
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>
@ParameterObject
is the answer:
import org.springdoc.core.annotations.ParameterObject;
// ...
@GetMapping
public ResponseEntity<List<UserResponseDto>> findUsers(@ParameterObject FindUserRequestDto userRequestDto) {
// ...
}