blog

vuePress-theme-reco blog    2019 - 2020
blog blog

Choose mode

  • dark
  • auto
  • light
Home
Category
  • Linux
  • java
  • java 后端
  • typora
  • vue
  • java 基础
  • 编程方法
  • Mysql
Tag
TimeLine
在线工具
  • PDF 转换器
  • JSON 编辑器
  • MD 表格生成器
  • CRON 表达式
  • 代码格式化
  • 公式编辑器
  • 二维码生成器
  • 在线编码转换
  • YAML <-> Properties
  • 在线 Web 练习
Contact
  • GitHub
  • 简书
  • CSDN
  • 博客圆
  • WeChat
author-avatar

blog

23

文章

16

标签

Home
Category
  • Linux
  • java
  • java 后端
  • typora
  • vue
  • java 基础
  • 编程方法
  • Mysql
Tag
TimeLine
在线工具
  • PDF 转换器
  • JSON 编辑器
  • MD 表格生成器
  • CRON 表达式
  • 代码格式化
  • 公式编辑器
  • 二维码生成器
  • 在线编码转换
  • YAML <-> Properties
  • 在线 Web 练习
Contact
  • GitHub
  • 简书
  • CSDN
  • 博客圆
  • WeChat
  • Swagger 快速上手

    • 1.pom
      • 2.SwaggerConfig
        • 3.WebMvcConfig
          • 4.Application
            • 5.注解详情

            Swagger 快速上手

            vuePress-theme-reco blog    2019 - 2020

            Swagger 快速上手


            blog 2019-10-01 Swagger

            # Swagger 快速上手

            # 1.pom

            1569607800479

            		<dependency>
                        <groupId>io.springfox</groupId>
                        <artifactId>springfox-swagger-ui</artifactId>
                        <version>2.9.2</version>
                    </dependency>
            
                    <dependency>
                        <groupId>io.springfox</groupId>
                        <artifactId>springfox-swagger2</artifactId>
                        <version>2.9.2</version>
                    </dependency>
            
                    <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-core</artifactId>
                        <version>2.9.8</version>
                    </dependency>
            
            1
            2
            3
            4
            5
            6
            7
            8
            9
            10
            11
            12
            13
            14
            15
            16
            17

            # 2.SwaggerConfig

            1569607116194

            package com.smxy.rxt.common.config;
            
            import org.springframework.context.annotation.Bean;
            import org.springframework.context.annotation.ComponentScan;
            import org.springframework.context.annotation.Configuration;
            import org.springframework.web.servlet.config.annotation.EnableWebMvc;
            import springfox.documentation.builders.ApiInfoBuilder;
            import springfox.documentation.builders.PathSelectors;
            import springfox.documentation.builders.RequestHandlerSelectors;
            import springfox.documentation.service.ApiInfo;
            import springfox.documentation.service.Contact;
            import springfox.documentation.spi.DocumentationType;
            import springfox.documentation.spring.web.plugins.Docket;
            import springfox.documentation.swagger2.annotations.EnableSwagger2;
            
            @Configuration
            @EnableSwagger2
            @EnableWebMvc
            @ComponentScan(basePackages = {"com.smxy.rxt.sys.controller"})
            public class SwaggerConfig {
                /**
                 * 创建API应用
                 * apiInfo() 增加API相关信息
                 * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
                 * 本例采用指定扫描的包路径来定义指定要建立API的目录。
                 *
                 * @return
                 */
                @Bean
                public Docket createRestApi() {
                    return new Docket(DocumentationType.SWAGGER_2)
                            .apiInfo(apiInfo())
                            .select()
                            .apis(RequestHandlerSelectors.basePackage("com.smxy.rxt.sys.controller"))
                            .paths(PathSelectors.any())
                            .build();
                }
            
                /**
                 * 创建该API的基本信息(这些基本信息会展现在文档页面中)
                 * 访问地址:http://项目实际地址/swagger-ui.html
                 * @return
                 */
                private ApiInfo apiInfo() {
                    Contact contact = new Contact("RXT","http://www.baidu.com" ,"958465427@qq.com");
                    return new ApiInfoBuilder()
                            .title("Ruan.X.T.的项目接口文档")
                            .description("API接口")
                            .termsOfServiceUrl("http://www.baidu.com")
                            .contact("sunf")
                            .version("1.0")
                            .build();
                }
            }
            
            
            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
            52
            53
            54
            55

            # 3.WebMvcConfig

            1569607177089

            package com.smxy.rxt.common.config;
            
            import com.smxy.rxt.common.interceptor.LoginInterceptor;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.context.annotation.Configuration;
            import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
            import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
            import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
            
            @Configuration
            public class WebConfigurer implements WebMvcConfigurer {
                @Autowired
                private LoginInterceptor loginInterceptor;
                @Override
                public void addInterceptors(InterceptorRegistry registry) {
                    registry.addInterceptor(loginInterceptor).addPathPatterns("/aaa")
                            .excludePathPatterns("/loginToIndex","/regist","/login", "/css/**", "/img/**", "/js/**", "/plugin/**", "/fonts/**","/iconfont/**","/images/**","/lib/**","/static/**","/web/**","/alipay/**","/swagger-ui.html");
                }
                @Override
                public void addResourceHandlers(ResourceHandlerRegistry registry) {
                    registry.addResourceHandler("swagger-ui.html")
                            .addResourceLocations("classpath:/META-INF/resources/");
            
                    registry.addResourceHandler("webjars/**")
                            .addResourceLocations("classpath:/META-INF/resources/webjars/");
            
                    registry.addResourceHandler("/**")
                            .addResourceLocations("classpath:/static/");
                }
            }
            
            
            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

            # 4.Application

            1569607207592

            # 5.注解详情

            1569607228008

            1569607245447