3

我试图弄清楚如何指示SpringDoc OpenApi正在生成一个自定义对象,使用HandlerMethodArgumentResolver它拦截一些传入的请求参数并从中创建一个对象。这个概念实际上与 Spring 对@Pageable.

这是我的自定义对象的一部分:

class FlightRequestHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
FlightRequestHandlerMethodArgumentResolver() {
}

@Override
boolean supportsParameter(final MethodParameter parameter) {
    return FlightRequest.class.equals(parameter.getParameterType());
}

@Override
Object resolveArgument(
        final MethodParameter parameter,
        final ModelAndViewContainer mavContainer,
        final NativeWebRequest webRequest, final WebDataBinderFactory binderFactory) throws Exception {
    final Map<String, String[]> parameterMap = webRequest.getParameterMap()
    // query param names are configurable
    // implementation about finding query param names is ommited
    return new FlightRequest(parameterMap)
}

FlightRequest班级:

public class FlightRequest {

@Parameter(name = "startDate")
private final LocalDate startDate;

@Parameter(name = "endDate")
private final LocalDate endDate;

FlightRequest (LocalDate startDate, LocalDate endDate) {
    this.submissionStartDate = submissionStartDate;
    this.submissionEndDate = submissionEndDate;
}

FlightRequest使用默认查询参数名称的控制器:

@RequestMapping(value = "/flights", method = RequestMethod.GET)
Flights flights(FlightRequest flightRequest, HttpServletResponse response) {
    return flightSearchService.retrieveFlights(flightRequest);
}

FlightRequest与自定义查询参数名称一起使用的控制器:

@RequestMapping(value = "/current-flights", method = RequestMethod.GET)
Flights flights(@FlightRequestParams(fromDateParamName="current-flights-from", toDateParamName="current-flights-till) FlightRequest flightRequest, HttpServletResponse response) {
    return flightSearchService.retrieveFlights(flightRequest);
}

就像您在上面的示例中看到的那样,每个端点都可以覆盖默认查询参数名称。
我怎样才能表明这一点Spring Doc

4

1 回答 1

0

看来您在这里使用的不是弹簧注释,而是您自己的:@FlightRequestParams。

如果您真的希望动态添加默认值,那么您可以为您的注解DelegatingMethodParameterCustomizer编写您自己的默认值,为您的注解,就像对@PageableDefault. 然后将其声明为 spring Bean,如下所示

或者最简单的方法是使用@Parameter注解声明参数值。您可以看到PageableAsQueryParam的示例。

于 2020-12-02T11:12:40.190 回答