Spring boot 整合Swagger
阿新 • • 發佈:2018-12-12
簡介
Swagger 是一款RESTFUL介面的文件線上自動生成+功能測試功能軟體。本文簡單介紹了在專案中整合swagger的方法。 Swagger 是一個規範和完整的框架,用於生成、描述、呼叫和視覺化 RESTful 風格的 Web 服務。總體目標是使客戶端和檔案系統作為伺服器以同樣的速度來更新。檔案的方法,引數和模型緊密整合到伺服器端的程式碼,允許API來始終保持同步。Swagger 讓部署管理和使用功能強大的API從未如此簡單。
環境
- Spring boot 2.0.5
- Swagger springfox-swagger-ui 2.6.1
- springfox-swagger2 2.6.1
步驟
新建Spring boot專案
新增Swagger依賴
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
新增Swagger配置類
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
. apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.imjcker.springswagger.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("介面文件註釋")
.description("介面文件註釋說明")
.termsOfServiceUrl("幫助連線")
.contact(new Contact("imjcker", "http://imjcker.com", "[email protected]"))
.version("1.0")
.build();
}
}
測試
@RestController
@Api(description = "測試介面")
public class TestController {
@ApiOperation("測試方法")
@GetMapping("/doTest")
public String test(String param1) {
System.out.println("param1 = [" + param1 + "]");
return "TestController.test";
}
}
結果展示
總結
更詳盡的配置,請參考官方網站!