1. 新建項目
2. 導入相關依賴
<!-- 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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
3. 編寫一個示例程序
4. 編寫 Swagger配置
5. 測試運行
http://localhost:8080/swagger-ui.html#/
6. 配置 Swagger
@Configuration
@EnableSwagger2 // 開啓 Swagger2
public class SwaggerConfig {
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
public ApiInfo apiInfo(){
Contact contact = new Contact("cxx", "#", "[email protected]");
return new ApiInfo(
"Swagger API 文檔",
"略",
"1.0",
"#",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}