gpt4 book ai didi

spring - 可分页未在 swagger 中正确显示

转载 作者:行者123 更新时间:2023-12-02 16:21:57 31 4
gpt4 key购买 nike

当我使用 Spring Boot 将 swagger 更新为 swagger2 时,它在应该显示 pagesize 时停止显示 pageable 类型的正确参数> 相反,它开始显示 pageSizepageNumber 这在其余部分不正确。

enter image description here

我没有手动更改任何内容,但由于某种原因,它显示了错误的参数名称。

 return new Docket(DocumentationType.SWAGGER_2)
.groupName("Rest API")
.securitySchemes(Collections.singletonList(new BasicAuth(BASIC_AUTH)))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.paths(s -> oneOf(
"/some/**",
"/search-controller/**").test(s))
.build();

pom 是

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>2.9.0</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.0</version>
</dependency>

Controller 如下所示

@RequestMapping(method = RequestMethod.GET)
public HttpEntity<?> findAll(@RequestParam(value = "countryIsoAlpha2", required = false) final String countryKey, final Pageable pageable){

}

最佳答案

https://github.com/springfox/springfox/issues/755#issuecomment-393378205

下面是创建规则的示例,该规则自动提供配置可分页类型的约定。

@Configuration
public class SwaggerConfig {

@Bean
public AlternateTypeRuleConvention pageableConvention(
final TypeResolver resolver) {
return new AlternateTypeRuleConvention() {

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

@Override
public List<AlternateTypeRule> rules() {
return Arrays.asList(
newRule(resolver.resolve(Pageable.class), resolver.resolve(pageableMixin()))
);
}
};
}

private Type pageableMixin() {
return new AlternateTypeBuilder()
.fullyQualifiedClassName(
String.format("%s.generated.%s",
Pageable.class.getPackage().getName(),
Pageable.class.getSimpleName()))
.withProperties(Arrays.asList(
property(Integer.class, "page"),
property(Integer.class, "size"),
property(String.class, "sort")
))
.build();
}

private AlternateTypePropertyBuilder property(Class<?> type, String name) {
return new AlternateTypePropertyBuilder()
.withName(name)
.withType(type)
.withCanRead(true)
.withCanWrite(true);
}
}

关于spring - 可分页未在 swagger 中正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50676841/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com