SpringBoot下Swagger配置

1.下在git载swagger ui的源码 https://github.com/swagger-api/swagger-uigit

2.将源码中的dist文件夹下的文件考到main/weapp/swagger 目录下github

 

3.gradle添加依赖,更新build.gradleweb

//swagger依赖spring

           compile('io.springfox:springfox-swagger2:2.8.0')api

compile('io.springfox:springfox-swagger-ui:2.8.0')springboot

compile 'io.swagger:swagger-jersey2-jaxrs:1.5.8'mvc

compile('com.mangofactory:swagger-springmvc:1.0.2')app

compile('com.mangofactory:swagger-models:1.0.2')gradle

compile('com.wordnik:swagger-annotations:1.3.11')ui

 

 

 

 

 

4.在springboot的入口程序的相同目录下建立swagger配置文件SwaggerConfig

 

 

package app;

 

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

 

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

public class SwaggerConfig {

  public static final String SWAGGER_SCAN_BASE_PACKAGE = "app.Controller";

    public static final String VERSION = "1.0.0";

 

    @Bean

    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)

                .apiInfo(apiInfo())

                .select()

                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))//api接口包扫描路径

                .paths(PathSelectors.any())//能够根据url路径设置哪些请求加入文档,忽略哪些请求

                .build();

    }

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()

            .title("Swagger2 接口文档示例")//设置文档的标题

            .description("更多内容请关注:http://www.abc.com")//设置文档的描述->1.Overview

            .version(VERSION)//设置文档的版本信息-> 1.1 Version information

            .contact(new Contact("ABC Boot", "http://www.abc.comt", ""))//设置文档的联系方式->1.2 Contact information

            .termsOfServiceUrl("www.abc.com")//设置文档的License信息->1.3 License information

            .build();

    }

}

 

 

5.修改SpringBoot,添加@EnableSwagger2注解

 

@EnableSwagger2

@SpringBootApplication

public class Application   {

 

 

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

 

}

相关文章
相关标签/搜索