让 swagger-ui 显示我的代码生成 API

Getting swagger-ui to display my codegen-generated API

我正在做一个 springboot 项目。我们首先执行 API,因此我们从 api.yaml 生成代码。我们正在使用 openapi 3.0。接口生成正常,但是当我们浏览到 swagger-ui URL 时,它说没有在规范中定义操作!

详情如下:

@Configuration
@RequiredArgsConstructor
@EnableSwagger2
public class SwaggerConfig {

  private final BuildProperties buildProperties;

  @Bean
  public Docket docketConfig() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors
            .basePackage("com.xyz.infrastructure.rest.spring.resource"))
        .build().apiInfo(apiInfo());
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title(buildProperties.getName())
        .version(buildProperties.getVersion())
        .build();
  }
}

我们的结构是:

com.xyz.infrastructure.rest.spring | |- 配置 |- SwaggerConfig |- spec //自动生成 |- dto //自动生成 |- resource // 在规范

中找到的接口实现

我们缺少什么?

我们正在使用:

<dependency>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen</artifactId>
  <version>3.0.21</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>

谢谢!

我使用如下:

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                //.apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("uz.xb.qr_project"))//*** base package
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metaData());
    }

    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("Spring Boot REST API")
                .description("\"Spring Boot REST API for Online Store\"")
                .version("1.0.0")
                .license("Apache License Version 2.0")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
                .contact(new Contact("John Thompson", "https://springframework.guru/about/", "john@springfrmework.guru"))
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")//**address of swagger ui
                .addResourceLocations("classpath:/META-INF/resources/");


        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

ApiController.java

@RestController
@RequestMapping
public class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping
    public String swagger(){
        return "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>REST API LIST</title>\n" +
                "    <meta http-equiv=\"refresh\" content=\"0; url=/qr_online/swagger-ui.html\" />\n" +
                "</head>\n" +
                "<body>\n" +
                "<a href=\"qr_online/swagger-ui.html\">REST API LIST</a>\n" +
                "</body>\n" +
                "</html>";
    }

  //Or you can use like this inside controller
  /*
    @GetMapping
    public void redirect(HttpServletResponse response) throws IOException {
        response.sendRedirect("swagger-ui.html");
    }
  */

}

pom.xml

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

我们终于让它工作了。这是为我们做的:

Springfox 版本 3:

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>

使用 io.swagger.codegen.v3 插件。

这个 swagger 配置:

@Configuration
@EnableOpenApi
public class SwaggerConfig {

  @Bean
  public Docket docketConfig() {
    return new Docket(DocumentationType.OAS_30)
        .select()
        .apis(RequestHandlerSelectors
            .basePackage("com.xyz.infrastructure.rest.spring.resource"))
        .build();
  }

}

并确保资源中的 类 具有 @RestController 注释。