【原】Spring Boot 配置swagger2没有文档解决方案

时间:2022-04-16 06:56:28
@Bean
public Docket customImplementation(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xx.controller"))
.build()
.directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
.directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class)
.apiInfo(apiInfo());
}

#如上图所示,使用basePackage扫描com.xx.controller,启动项目后访问http://127.0.0.1:8088/swagger-ui.html,页面可以出来就是接口文档出不来。于是替换如下:

 @Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.build();
}

将basePackage扫描的条件改为:RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class),通过扫描ApiOperation注解修饰的Controller。