swagger 是一個功能強大的api框架,不僅提供了在線文檔的查閱,而且還提供了在線文檔的測試
1、引入依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2、創建配置類
我們創建一個swagger2Config文件
@Configuration
@EnableSwagger2 // 開啓swagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 指定掃描的包
.apis(RequestHandlerSelectors.basePackage("com.wzp.ljy"))
.paths(PathSelectors.any())
.build();
}
// 配置一些基本的信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swaggerApi文檔")
.description("http://www.wzp.com/wzp")
.termsOfServiceUrl("http://www.wzp.com/wzp")
.version("1.0")
.build();
}
}
3、一個簡單的例子
@RestController
@Api(tags = "公共接口管理")
public class commonController {
/**
* 獲取系統時間
* @return
*/
@GetMapping("/getTime")
@ApiOperation("獲取系統時間")
public Map Time(){
Long sysTime = System.currentTimeMillis();
Map<String,Long> map = new HashMap<>();
map.put("sysTime",sysTime);
return map;
}
啓動工程,訪問:http://localhost:8081/swagger-ui.html 可以看到如下界面,則說明swagger生效成功
4、附上一份swagger相關注解的解釋
@Api:修飾整個類,描述Controller的作用
@ApiOperation:描述一個類的一個方法,或者說一個接口
@ApiParam:單個參數描述
@ApiModel:用對象來接收參數
@ApiProperty:用對象接收參數時,描述對象的一個字段
@ApiResponse:HTTP響應其中1個描述
@ApiResponses:HTTP響應整體描述
@ApiIgnore:使用該註解忽略這個API
@ApiError :發生錯誤返回的信息
@ApiParamImplicitL:一個請求參數
@ApiParamsImplicit 多個請求參數
5、結語
按照慣例寫個結語,嗯...看了很多大佬的教程,結合我自己的總結了一波,當然了,不足之處請多包涵,也請多指教...如有雷同,也請多包涵......