zoukankan      html  css  js  c++  java
  • springboot集成swagger2及需要注意的坑

    springboot集成swagger2及需要注意的坑

    1、springboot集成swagger2

    1.1 swagger的ui界面效果--简洁好用

    1.2 pom文件中添加swagger依赖

           <!-- swagger2 -->
            <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>
    

    1.3 编写相关配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurationSupport {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.jerry.first.controller"))
                    .paths(PathSelectors.any()).build();
        }
    
        /**
         * 配置在线文档的基本信息
         * @return
         */
        public ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("参数校验和统一异常处理Demo")
                    .description("用来演示参数校验和统一异常处理")
                    .termsOfServiceUrl("rudecrab.com")
                    .contact(new Contact("RudeCrab", "", "rudecrab@foxmail.com"))
                    .version("1.0")
                    .build();
        }
    
    }
    

    1.4 这样swagger2与springboot就集成完毕了。

    通过浏览器访问路径: http://localhost:8080/swagger-ui.html ,若顺利,就会出现文章开始的时候的这种界面。

    如果没有出现swagger的界面,此时可以看到控制台会报出 No mapping for GET /swagger-ui.html的错误,这时大概率时由于我们的项目中的其他自定义的配置类集成了WebMvcConfigurationSupport类,导致swagger的相应配置失效。。。

    解决办法:在swagger配置文件下重写WebMvcConfigurationSupport类的addResourceHandlers方法

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurationSupport {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.jerry.first.controller"))
                    .paths(PathSelectors.any()).build();
        }
    
        /**
         * 配置在线文档的基本信息
         * @return
         */
        public ApiInfo apiInfo(){
            return new ApiInfoBuilder()
                    .title("参数校验和统一异常处理Demo")
                    .description("用来演示参数校验和统一异常处理")
                    .termsOfServiceUrl("rudecrab.com")
                    .contact(new Contact("RudeCrab", "", "rudecrab@foxmail.com"))
                    .version("1.0")
                    .build();
        }
        
     *************** 重写addResourceHandlers ************************************
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/**").addResourceLocations(
                    "classpath:/static/");
            registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                    "classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
            super.addResourceHandlers(registry);
        }
     }
    

    再重启项目,问题解决。

  • 相关阅读:
    IIS7中的几种身份鉴别方式(一)Basic身份验证
    IIS7中的几种身份鉴别方式(二)集成身份验证
    java集合
    SharePoint 2010中welcome page的设置细节
    SharePoint中使用Linq出现未将对象引用到实例化的解决方法
    SharePoint 2010中关于An error was encountered while retrieving the user profile的处理方式记录
    The Need for an Architectural Body of Knowledge
    The Softer Side of the Architect
    Event Receivers 学习小结
    使用SmtpClient发送带图片的邮件的代码实现
  • 原文地址:https://www.cnblogs.com/niew/p/14514921.html
Copyright © 2011-2022 走看看