• springboot集成swagger3.0


    springboot集成swagger3.0

    不同swagger版本访问路径

    swagger版本访问路径
    3.0.0之前http://127.0.0.1:8080/swagger-ui.html
    3.0.0之后http://127.0.0.1:8080/swagger-ui/index.html

    swagger不同版本启动类配置注解

    swagger版本启动类配置注解
    3.0.0之前@EnableSwagger2
    3.0.0之后@EnableOpenApi

    引入依赖

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-boot-starter</artifactId>
                <version>3.0.0</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    yml配置

    启动时如果出现以下报错Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException,需要在yml添加如下配置

    spring:
      mvc:
        path-match:
          matching-strategy: ant_path_matcher
    
    • 1
    • 2
    • 3
    • 4

    swagger配置类

    swagger2.0和3.0指定的DocumentationType是不同的,2.0是SWAGGER_2,3.0是OAS_30;
    Spring Boot 6.x 与Swagger 3.0.0 不兼容问题,会出现报错Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException,解决该问题方案如下:

    @Configuration
    public class SwaggerConfig {
    
        //访问http://127.0.0.1:1003/swagger-ui/index.html可以看到API文档
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.OAS_30)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("test")
                    .description("test模块")
                    .version("1.0")
                    .license("转载请保留原文作者@Maruko")
                    .licenseUrl("https://blog.csdn.net/qq_35705176/article/details/127994334?spm=1001.2014.3001.5502")
                    .termsOfServiceUrl("")
                    .build();
        }
    
        /**
         * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
         **/
        @Bean
        public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
                                                                             ServletEndpointsSupplier servletEndpointsSupplier,
                                                                             ControllerEndpointsSupplier controllerEndpointsSupplier,
                                                                             EndpointMediaTypes endpointMediaTypes,
                                                                             CorsEndpointProperties corsProperties,
                                                                             WebEndpointProperties webEndpointProperties,
                                                                             Environment environment) {
            List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
            Collection<ExposableWebEndpoint> 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);
        }
        private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
            return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    碰到问题

    在集成swagger3.0时,碰到一个问题,Request body参数示例为空,后面发现是因为添加了懒加载的原因,需要去掉该配置

    spring:
      application:
        name: producer-develop
    #  main:
    #    lazy-initialization: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    Springboot:静态资源映射方式
    UGUI性能优化学习笔记(番外)一些零星的优化点
    netty-reacter写一个http服务器
    C++(CMake)视觉OpenCV-Raspberry Pi图像处理-3D图像重建-面部界标检测-卷积神经网络车牌自动识别-深度神经网络面部检测和识别
    vue 前端 问题整理
    这怎么回事?宝宝边吃母乳边用手推妈妈,宝宝的心思知多少
    递推算法 C++
    【概率论】条件概率、贝叶斯公式、相关系数、中心极限定理、参数估计、假设检验
    Flume安装部署
    超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
  • 原文地址:https://blog.csdn.net/qq_35705176/article/details/128100765