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";
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);
}
}