詳解Swagger接口文檔和常用注解的使用
Swagger是一款遵循 Restful 風(fēng)格的接口文檔開(kāi)發(fā)神器,支持基于 API 自動(dòng)生成接口文檔,接口文檔始終與 API 保持同步,不再需要手動(dòng)編寫(xiě)接口文檔,并且采用全注解的方式,開(kāi)發(fā)簡(jiǎn)單,代碼侵入性低,對(duì)服務(wù)端開(kāi)發(fā)的程序員來(lái)說(shuō)非常方便,可以節(jié)約大量寫(xiě)接口文檔的時(shí)間。除此之外,Swagger 生成的文檔還支持在線測(cè)試,參數(shù)和格式都定好了,直接在界面上輸入?yún)?shù)對(duì)應(yīng)的值即可在線測(cè)試接口。
一、Spring整合Swagger
(1)在 pom.xml 文件中添加 Swagger 的 maven 依賴:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.4.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.4.0</version> </dependency>
(2)編寫(xiě)Swagger自定義配置類:
/** * 類說(shuō)明 :自定義swagger配置信息 */ @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket creatApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //選擇哪些路徑和api會(huì)生成document .apis(RequestHandlerSelectors.basePackage("com.zwp.controller"))//controller路徑 //.apis(RequestHandlerSelectors.any()) //對(duì)所有api進(jìn)行監(jiān)控 .paths(PathSelectors.any()) //對(duì)所有路徑進(jìn)行監(jiān)控 .build(); } //接口文檔的一些基本信息 private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springmvc+swagger整合")//文檔主標(biāo)題 .description("test接口文檔")//文檔描述 .version("1.0.0")//API的版本 .termsOfServiceUrl("###") .license("LICENSE") .licenseUrl("###") .build(); } }
在 springmvc.xml 文件中配置創(chuàng)建對(duì)象:
<!-- 使用注解驅(qū)動(dòng):自動(dòng)配置處理器映射器與處理器適配器 --> <mvc:annotation-driven /> <!-- 注解方式:自動(dòng)掃描該包 --> <context:component-scan base-package="com.zwp.config" />
(3)在 springmvc.xml 中過(guò)濾掉 swagger-ui 的靜態(tài)資源文件:
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" /> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
(4)在controller類添加swagger的注解:
//在類上面加@Api注解,表明該controller類需要被swagger自動(dòng)生成文檔 @Api(tags="UserController",description="user的控制層") @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; //在方法上面添加@ApiOperation注解,表明該方法需要被swagger自動(dòng)生成文檔 @ApiOperation(httpMethod="GET",value="接口標(biāo)題:獲取用戶信息",notes="接口的notes說(shuō)明:需要user的id") @RequestMapping(value="/getUserById/{userId}",method=RequestMethod.GET) public @ResponseBody User getUserById(@PathVariable Integer userId){ return userService.getUserById(userId); } }
(5)部署工程,訪問(wèn)路徑:
格式:http://服務(wù)器ip:端口/項(xiàng)目名稱//swagger-ui.html
例子:http://localhost:8080/ssm/swagger-ui.html
見(jiàn)到上面頁(yè)面,表示整合成功。
二、swagger常用注解說(shuō)明
注解 | 說(shuō)明 |
@Api | 修飾controller類,標(biāo)識(shí)這個(gè)類是swagger的資源 |
@ApiOperation | 修飾controller的方法,表示一個(gè)http請(qǐng)求的操作 |
@ApiParam | 修飾方法的參數(shù),用于添加參數(shù)說(shuō)明與是否必填等元數(shù)據(jù) |
@ApiModel | 修飾對(duì)象類,表示對(duì)對(duì)象類進(jìn)行說(shuō)明,用于參數(shù)用實(shí)體類接收的場(chǎng)景 |
@ApiModelProperty | 修飾對(duì)象類中的屬性,對(duì)屬性進(jìn)行說(shuō)明 |
@ApiIgnore() | 修飾類、方法、參數(shù)等,表示不顯示在swagger文檔中 |
@ApiImplicitParam | 用于方法,表示單獨(dú)的請(qǐng)求參數(shù) |
@ApiImplicitParams | 用于方法,包含多個(gè) @ApiImplicitParam |
1、@Api的使用說(shuō)明
修飾controller類,標(biāo)識(shí)這個(gè)類是swagger的資源,屬性說(shuō)明:
tags:類的說(shuō)明,但是tags如果有多個(gè)值,會(huì)生成多個(gè)list
value:也是類的說(shuō)明,可以使用tags替代
@Api(value="用戶controller",tags={"用戶操作接口"}) @RestController public class UserController { }
效果圖:
2、@ApiOperation的使用說(shuō)明
修飾controller的方法,表示一個(gè)http請(qǐng)求的操作,屬性說(shuō)明:
value:用于方法描述
notes:用于提示內(nèi)容
tags:可以重新分組,視情況而用)
3、@ApiParam的使用說(shuō)明
修飾方法的參數(shù),用于添加參數(shù)說(shuō)明與是否必填等元數(shù)據(jù),屬性說(shuō)明:
name:參數(shù)名
value:參數(shù)說(shuō)明
required:是否必填
@Api(value="用戶controller",tags={"用戶操作接口"}) @RestController public class UserController { @ApiOperation(value="獲取用戶信息",tags={"獲取用戶信息copy"},notes="注意問(wèn)題點(diǎn)") @GetMapping("/getUserInfo") public User getUserInfo(@ApiParam(name="id",value="用戶id",required=true) Long id,@ApiParam(name="username",value="用戶名") String username) { // userService可忽略,是業(yè)務(wù)邏輯 User user = userService.getUserInfo(); return user; } }
效果圖:
4、@ApiModel的使用說(shuō)明
修飾對(duì)象類,表示對(duì)對(duì)象類進(jìn)行說(shuō)明,用于參數(shù)用實(shí)體類接收的場(chǎng)景,屬性說(shuō)明:
value:表示對(duì)象名,可省略
description:描述,可省略
5、@ApiModelProperty的使用說(shuō)明
修飾對(duì)象類中的屬性,對(duì)屬性進(jìn)行說(shuō)明,屬性說(shuō)明:
- value:字段說(shuō)明
- name:重寫(xiě)屬性名字
- dataType:重寫(xiě)屬性類型
- required:是否必填
- example:舉例說(shuō)明
- hidden:是否隱藏
@ApiModel(value="user對(duì)象",description="用戶對(duì)象user") public class User implements Serializable{ private static final long serialVersionUID = 1L; @ApiModelProperty(value="用戶名",name="username",example="xingguo") private String username; @ApiModelProperty(value="狀態(tài)",name="state",required=true) private Integer state; private String password; private String nickName; private Integer isDeleted; @ApiModelProperty(value="id數(shù)組",hidden=true) private String[] ids; private List<String> idList; }
@ApiOperation("更改用戶信息") @PostMapping("/updateUserInfo") public int updateUserInfo(@RequestBody @ApiParam(name="用戶對(duì)象",value="傳入json格式",required=true) User user){ int num = userService.updateUserInfo(user); return num; }
效果圖:
6、@ApiIgnore的使用說(shuō)明
修飾類、方法、參數(shù)等,表示不顯示在swagger文檔中,比較簡(jiǎn)單, 這里不做舉例
7、@ApiImplicitParam的使用說(shuō)明
用于方法,表示單獨(dú)的請(qǐng)求參數(shù)
8、@ApiImplicitParams的使用說(shuō)明
用于方法,包含多個(gè) @ApiImplicitParam,屬性說(shuō)明:
- name:參數(shù)ming
- value:參數(shù)說(shuō)明
- dataType:數(shù)據(jù)類型
- paramType:參數(shù)類型
- example:舉例說(shuō)明
@ApiOperation("查詢測(cè)試") @GetMapping("select") //@ApiImplicitParam(name="name",value="用戶名",dataType="String", paramType = "query") @ApiImplicitParams({ @ApiImplicitParam(name="name",value="用戶名",dataType="string", paramType = "query",example="xingguo"), @ApiImplicitParam(name="id",value="用戶id",dataType="long", paramType = "query")}) public void select(){ }
效果圖:
9、@ApiResponses與@ApiResponse使用說(shuō)明
這兩個(gè)注解都表示對(duì)響應(yīng)結(jié)果進(jìn)行說(shuō)明
10、@RequestMapping注解的推薦配置
value、method、produces
示例:
@ApiOperation("信息軟刪除") @ApiResponses({ @ApiResponse(code = CommonStatus.OK, message = "操作成功"), @ApiResponse(code = CommonStatus.EXCEPTION, message = "服務(wù)器內(nèi)部異常"), @ApiResponse(code = CommonStatus.FORBIDDEN, message = "權(quán)限不足") }) @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", dataType = "Long", name = "id", value = "信息id", required = true) }) @RequestMapping(value = "/remove.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public RestfulProtocol remove(Long id) {
以上就是詳解Swagger接口文檔和常用注解的使用的詳細(xì)內(nèi)容,更多關(guān)于Swagger接口文檔 注解的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
獲取JPEGImageEncoder和JPEGCode這兩個(gè)類的方法
下面小編就為大家?guī)?lái)一篇獲取JPEGImageEncoder和JPEGCode這兩個(gè)類的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Jmeter使用接口傳遞數(shù)據(jù)過(guò)程圖解
這篇文章主要介紹了Jmeter使用接口傳遞數(shù)據(jù)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Spring中使用騰訊云發(fā)送短信驗(yàn)證碼的實(shí)現(xiàn)示例
本文主要介紹了Spring?中?使用騰訊云發(fā)送短信驗(yàn)證碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能
這篇文章主要介紹了springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能,本文通過(guò)示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07詳解java中的PropertyChangeSupport與PropertyChangeListener
這篇文章主要介紹了詳解java中的PropertyChangeSupport與PropertyChangeListener的相關(guān)資料,需要的朋友可以參考下2017-09-09spring cloud 使用Zuul 實(shí)現(xiàn)API網(wǎng)關(guān)服務(wù)問(wèn)題
這篇文章主要介紹了spring cloud 使用Zuul 實(shí)現(xiàn)API網(wǎng)關(guān)服務(wù)問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05