springboot2.6以上兼容swagger3.0问题 knife4j风格

1.pom文件

/*knife4j-spring-boot-starter这个jar包包含了 swagger依赖

    io.springfox
    springfox-swagger2
    3.0.0


    io.springfox
    springfox-swagger-ui
    3.0.0
*/

    com.github.xiaoymin
    knife4j-spring-boot-starter
    3.0.2

2.yml配置文件添加

spring: mvc: pathmatch: matching-strategy: ant_path_matcher

3.swagger配置类,兼容问题的那段代码 我试了下,可以去掉

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        ApiSelectorBuilder builder = new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.xx.xx.controller"))
                .paths(PathSelectors.any());
        return builder.build();
    }

    private static ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xx服务")
                .description("API文档")
                .termsOfServiceUrl("http://localhost:8888/doc.html")
                .version("1.0")
                .build();
    }

    // 解决兼容问题,必须添加
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                             ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier,
                             EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
                             WebEndpointProperties webEndpointProperties, Environment environment) {
        List> allEndpoints = new ArrayList();
        Collection webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(),
                new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }

    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }

}

你可能感兴趣的:(springmvc,框架,java,spring,spring,boot)