SpringBoot整合Swagger
本文章是SpringBoot對Swagger的簡單整合
- 引入整合Swagger的依賴:
基於能夠訪問Web的SpringBoot項目
<!-- 配置swagger2 -->
<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>
- 添加Swagger的Config類:
package com.example.swagger;
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 {
//http://localhost:8080/swagger-ui.html
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 當前包路徑
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any()).build();
}
//構建api文檔的詳細信息函數
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
//頁面標題
.title("Swagger2構建RESTful API")
//創建人
.contact(new Contact("Leay Lu","https://blog.csdn.net/l13568","歡迎訪問"))
//版本號
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
- 訪問Swagger的API網頁:
訪問: http://127.0.0.1:8080/swagger-ui.html