Springboot2.6+Swagger2 cannot scan the interface: No operations defined in spec!

First check that the swagger scan is enabled, enable: true; after the scan path is configured correctly, it still does not display

Springboot2.6+swagger2.9.2 can not scan the interface problem solution
In a multi-module environment, swaggerConfig is configured in the public module, and it is found that some modules cannot scan the Controller interface. As shown below:

After inspection, some modules forgot to add the following configuration:

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

Attach SwaggerConfig (found on the Internet)
dependency as springfox-boot-starter

@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {         return new Docket(DocumentationType.OAS_30)                 .enable(true) //Whether to enable swagger                 .apiInfo(apiInfo())                 //Set which interfaces are exposed to Swagger display.select                 ()                 //Scan all Annotated apis are more flexible in this way                 . apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // .paths(PathSelectors.any()) //This will filter all paths!                 .build() ;     }









    private ApiInfo apiInfo() {         // Customize with ApiInfoBuilder         return new ApiInfoBuilder()                 // Set title.title                 ("title")                 // Description.description                 ("Description")                 // Author                 information.contact(new Contact("username ", "website", "mail"))                 //                 version.version("version number:" + "1.0")                 .build();     }











    /**
     * Springboot2.6 will conflict with springfox even if it is equipped with ant_path_matcher
     * Solve the incompatibility between springboot2.6 and springfox Failed to start bean ' documentationPluginsBootstrapper ' ; nested exception...
     *
     * @return
     */
    @Bean
    public static BeanPostProcessor springfoxHand lerProviderBeanPostProcessor( ) {         return new BeanPostProcessor() {

            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
                }
                return bean;
            }

            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
                List<T> copy = mappings.stream()
                        .filter(mapping -> mapping.getPatternParser() == null)
                        .collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);
            }

            @SuppressWarnings("unchecked")
            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
                try {
                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                    field.setAccessible(true);

                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }

Guess you like

Origin blog.csdn.net/qq_34412985/article/details/131562054