美文网首页
五、集成swagger2

五、集成swagger2

作者: maololo | 来源:发表于2019-01-10 17:04 被阅读7次

    一、配置文件方式
    1、maven依赖

    <dependency>
        <groupId>com.spring4all</groupId>
        <artifactId>swagger-spring-boot-starter</artifactId>
        <version>1.8.0.RELEASE</version>
    </dependency>
    

    2、添加@EnableSwagger2Doc注解

    @SpringBootApplication
    @EnableSwagger2Doc
    public class SpringDataJpaApplication {
        public static void main(String[] args) {
            SpringApplication.run(SpringDataJpaApplication.class, args);
        }
    }
    

    3、定义配置文件application.yml

    # /swagger-ui.html
    swagger:
      enabled: true
      title: 后台管理平台
      description: Starter for swagger 2.x
      license: Apache License, Version 2.0
      licenseUrl: https://www.apache.org/licenses/LICENSE-2.0.html
      termsOfServiceUrl: https://github.com/dyc87112/spring-boot-starter-swagger
      contact:
        name: mll
        url: http://admin
        email: 123456@qq.com
      base-package: com.hn.basic.rest
      base-path: /**
      exclude-path: /error, /page
    

    二、实体bean方式定义配置
    1、maven依赖

            <!-- swagger -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
            </dependency>
    

    2、配置类

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        
        private static final String VERSION = "1.0";
    
        @Value("${swagger.title}")
        private String title;
        
        @Value("${swagger.description}")
        private String description;
        
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hn.lz.rest"))
                .paths(PathSelectors.any())
                .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .license("Apache 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .termsOfServiceUrl("")
                .version(VERSION)
                .build();
        }
    
    }
    

    三、访问路径ip:port/swagger-ui.html

    相关文章

      网友评论

          本文标题:五、集成swagger2

          本文链接:https://www.haomeiwen.com/subject/leiarqtx.html