小編給大家分享一下Spring Boot中怎么使用Swagger,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Swagger 是一個方便 API 開發的框架,它有以下優點:
自動生成在線文檔,后端開發人員的改動可以立即反映到在線文檔,并且不用單獨編寫接口文檔,減輕了開發負擔
支持通過 Swagger 頁面直接調試接口,方便前端開發人員進行測試
Swagger 目前有 2.x 和 3.x 兩個主流版本,配置略有不同。
首先去 Maven 倉庫中搜索 springfox 查找依賴的坐標,Swagger 是遵循 OpenAPI 規范的技術,而 springfox 是該技術的一種實現,所以這里要搜 springfox 而不是 swagger。
對于 Swagger 2.x,需要在 pom.xml 中添加兩項配置:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
對于 Swagger 3.x,簡化了配置項,只需要在 pom.xml 中添加一項配置:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
對于 Swagger 2.x,使用 @EnableSwagger2 注解開啟 Swagger 功能。
@EnableSwagger2
@SpringBootApplication
public class SwaggerApplication {
...
}對于 Swagger 3.x,使用 @EnableOpenApi 注解開啟 Swagger 功能。
@EnableOpenApi
@SpringBootApplication
public class SwaggerApplication {
...
}對于 Swagger 2.x,實例化 Docket 的時候,需要傳入 DocumentationType.SWAGGER_2。
對于 Swagger 3.x,實例化 Docket 的時候,需要傳入 DocumentationType.OAS_30。
下面是一份配置模板:
import org.springframework.beans.factory.annotation.Value;
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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
@Value("${spring.profiles.active:NA}")
private String active;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2) // OAS_30
.enable("dev".equals(active)) // 僅在開發環境開啟Swagger
.apiInfo(apiInfo())
.host("http://www.example.com") // Base URL
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("API文檔")
.description("這是描述信息")
.contact(new Contact("張三", null, null))
.version("1.0")
.build();
}
}對于 Swagger 2.x,訪問 http://localhost:8080/swagger-ui.html
對于 Swagger 3.x,訪問 http://localhost:8080/swagger-ui/
@Api:將一個類標記為 Swagger 資源,一般放在 Controller 類上面,通過 tags 指定描述信息,比如 @Api(tags="用戶管理")。
@ApiOperation:本注解放在 Controller 方法上面,描述該方法的作用。
@ApiParam:本注解放在 Controller 方法的形參前面,可以描述參數的作用,比如 @ApiParam("用戶名") String username??梢允褂?value 指定描述信息,通過 required = true 指定必需傳遞該參數。
package com.example.swagger.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "Hello控制器")
@RestController
public class HelloController {
@ApiOperation("展示歡迎信息")
@GetMapping("/hello")
public String hello(@ApiParam("名字") String name) {
return "hello, " + name;
}
}
@ApiModel:一般放在實體類上面??梢酝ㄟ^ value 指定別名,不指定時默認為類名。還可以通過 description 指定詳細的描述信息。比如 @ApiModel("用戶") 就將顯示 用戶 而不是 User。

如果僅僅想指定描述,而不改變原始類名顯示,可以寫成 @ApiModel(description = "用戶")。
@ApiModelProperty:一般放在實體類的成員變量上面,通過 value 指定描述信息,example 指定示例數據,required = true 指定該參數是必需的,hidden = true 用于隱藏該字段,不會在 API 文檔中顯示。
package com.example.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel(description = "用戶")
public class User {
@ApiModelProperty("用戶名")
private String username;
@ApiModelProperty("密碼")
private String password;
@ApiModelProperty(value = "年齡", example = "18", required = true)
private int age;
@ApiModelProperty(hidden = true)
private double money;
}
看完了這篇文章,相信你對“Spring Boot中怎么使用Swagger”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。