springboot 2.6.1 整合swagger3.0

1:swagger配置类

@Configuration
@EnableOpenApi
public class ApiSwaggerAutoConfiguration {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(ApiSwaggerProperties apiSwaggerProperties) {
        return new ApiInfoBuilder()
                .title("api文档")
                .build();
    }

2:增加一个bean,要不然项目无法启动

 @Bean
    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        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  void customizeSpringfoxHandlerMappings(List mappings) {
                List copy = mappings.stream()
                        .filter(mapping -> mapping.getPatternParser() == null)
                        .collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);
            }

            @SuppressWarnings("unchecked")
            private List getHandlerMappings(Object bean) {
                try {
                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                    field.setAccessible(true);
                    return (List) field.get(bean);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }

3: 配置文件设置默认值,不设置的话,看不到相关接口说明

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher 

4:增加maven依赖

   
        
            io.springfox
            springfox-boot-starter
            3.0.0
        

你可能感兴趣的:(springboot 2.6.1 整合swagger3.0)