将 Pageable 当成 Void 处理
@Bean
public AlternateTypeRuleConvention pageableConvention(final TypeResolver resolver) {
return new AlternateTypeRuleConvention() {
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
@Override
public List<AlternateTypeRule> rules() {
// 将 Pageable 当成 Void 处理,配合 PageableParamPlugin 进行处理
return Lists.newArrayList(AlternateTypeRules.newRule(resolver.resolve(Pageable.class), resolver.resolve(Void.class)));
}
};
}
在参数中有 Pageable 的接口中手动添加分页参数
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class PageableParamPlugin implements OperationBuilderPlugin {
private ResolvedType pageableType;
private List<Parameter> parameters;
private final TypeResolver resolver;
private final SpringDataWebProperties springDataWebProperties;
public PageableParamPlugin(TypeResolver resolver, SpringDataWebProperties springDataWebProperties) {
this.resolver = resolver;
this.springDataWebProperties = springDataWebProperties;
}
@PostConstruct
public void init() {
if (resolver == null || springDataWebProperties == null) {
return;
}
this.pageableType = resolver.resolve(Pageable.class);
ModelRef stringModel = new ModelRef("string");
ModelRef intModel = new ModelRef("int");
String parameterType = "query";
parameters = Lists.newArrayListWithExpectedSize(3);
parameters.add(new ParameterBuilder()
.parameterType(parameterType)
.name(springDataWebProperties.getPageable().getPageParameter())
.modelRef(intModel)
.description("当前页")
.build());
parameters.add(new ParameterBuilder()
.parameterType(parameterType)
.name(springDataWebProperties.getPageable().getSizeParameter())
.modelRef(intModel)
.description("查询的记录数")
.build());
parameters.add(new ParameterBuilder()
.parameterType(parameterType)
.name(springDataWebProperties.getSort().getSortParameter())
.modelRef(stringModel)
.allowMultiple(true)
.description("排序参数")
.build());
}
@Override
public void apply(OperationContext context) {
if (CollectionUtils.isEmpty(parameters)) {
return;
}
boolean hasPageableParam = context.getParameters()
.stream().anyMatch(parameter -> pageableType.equals(parameter.getParameterType()));
if (hasPageableParam) {
context.operationBuilder().parameters(parameters);
}
}
@Override
public boolean supports(DocumentationType delimiter) {
return true;
}
}
写的不好,仅供参考