美文网首页
SpringBoot整合swagger

SpringBoot整合swagger

作者: 蜻蜓队长家长 | 来源:发表于2019-07-09 16:18 被阅读0次

    一、swagger2官网

    swagger2 官网:https://swagger.io/

    二、maven 地址

    springfox-swagger2:http://mvnrepository.com/artifact/io.springfox/springfox-swagger2

    springfox-swagger-ui:http://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui

    三、在 pom.xml 添加依赖

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

    四、创建com.zhg.swagger.SwaggerConfig配置类

    package com.zhg.demo.swagger;
    
    
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.context.annotation.Bean;
    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.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    
    @Configuration
    @ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")//enable
    @EnableWebMvc
    public class SwaggerConfig {
    
        @Bean
        public Docket createRestApi() {
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.zhg.demo.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        @SuppressWarnings("deprecation")
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("测试swagger")
                    .description("RESTful APIs 接口文档集合")
                    .termsOfServiceUrl("http://www.lemos.club/")
                    .contact("zhg")//作者
                    .version("1.0")
                    //.license("The Apache License, Version 2.0")
                    .build();
        }
    
    }
    

    五、在springboot启动类中添加注解@EnableSwagger2

    package com.zhg.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication
    @EnableSwagger2
    public class SbApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SbApplication.class, args);
        }
    }
    

    六、测试

    -输入http://localhost:9091/swagger-ui.html#/测试

    swagger测试

    七、注意

    -在pom中不能缺少spring-boot-starter-web核心依赖

            <!--使用Spring MVC构建Web(包括RESTful)应用程序的入门者。使用Tomcat作为默认嵌入式容器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    

    相关文章

      网友评论

          本文标题:SpringBoot整合swagger

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