詳解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 生成的文檔還支持在線(xiàn)測(cè)試,參數(shù)和格式都定好了,直接在界面上輸入?yún)?shù)對(duì)應(yīng)的值即可在線(xiàn)測(cè)試接口。
一、Spring整合Swagger
(1)在 pom.xml 文件中添加 Swagger 的 maven 依賴(lài):
<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自定義配置類(lèi):
/**
* 類(lèi)說(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類(lèi)添加swagger的注解:
//在類(lèi)上面加@Api注解,表明該controller類(lèi)需要被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)題:獲取用戶(hù)信息",notes="接口的notes說(shuō)明:需要user的id")
@RequestMapping(value="/getUserById/{userId}",method=RequestMethod.GET)
public @ResponseBody User getUserById(@PathVariable Integer userId){
return userService.getUserById(userId);
}
}(5)部署工程,訪(fǎng)問(wèn)路徑:
格式:http://服務(wù)器ip:端口/項(xiàng)目名稱(chēng)//swagger-ui.html
例子:http://localhost:8080/ssm/swagger-ui.html

見(jiàn)到上面頁(yè)面,表示整合成功。
二、swagger常用注解說(shuō)明
| 注解 | 說(shuō)明 |
| @Api | 修飾controller類(lèi),標(biāo)識(shí)這個(gè)類(lèi)是swagger的資源 |
| @ApiOperation | 修飾controller的方法,表示一個(gè)http請(qǐng)求的操作 |
| @ApiParam | 修飾方法的參數(shù),用于添加參數(shù)說(shuō)明與是否必填等元數(shù)據(jù) |
| @ApiModel | 修飾對(duì)象類(lèi),表示對(duì)對(duì)象類(lèi)進(jìn)行說(shuō)明,用于參數(shù)用實(shí)體類(lèi)接收的場(chǎng)景 |
| @ApiModelProperty | 修飾對(duì)象類(lèi)中的屬性,對(duì)屬性進(jìn)行說(shuō)明 |
| @ApiIgnore() | 修飾類(lèi)、方法、參數(shù)等,表示不顯示在swagger文檔中 |
| @ApiImplicitParam | 用于方法,表示單獨(dú)的請(qǐng)求參數(shù) |
| @ApiImplicitParams | 用于方法,包含多個(gè) @ApiImplicitParam |
1、@Api的使用說(shuō)明
修飾controller類(lèi),標(biāo)識(shí)這個(gè)類(lèi)是swagger的資源,屬性說(shuō)明:
tags:類(lèi)的說(shuō)明,但是tags如果有多個(gè)值,會(huì)生成多個(gè)list
value:也是類(lèi)的說(shuō)明,可以使用tags替代
@Api(value="用戶(hù)controller",tags={"用戶(hù)操作接口"})
@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="用戶(hù)controller",tags={"用戶(hù)操作接口"})
@RestController
public class UserController {
@ApiOperation(value="獲取用戶(hù)信息",tags={"獲取用戶(hù)信息copy"},notes="注意問(wèn)題點(diǎn)")
@GetMapping("/getUserInfo")
public User getUserInfo(@ApiParam(name="id",value="用戶(hù)id",required=true) Long id,@ApiParam(name="username",value="用戶(hù)名") String username) {
// userService可忽略,是業(yè)務(wù)邏輯
User user = userService.getUserInfo();
return user;
}
}
效果圖:

4、@ApiModel的使用說(shuō)明
修飾對(duì)象類(lèi),表示對(duì)對(duì)象類(lèi)進(jìn)行說(shuō)明,用于參數(shù)用實(shí)體類(lèi)接收的場(chǎng)景,屬性說(shuō)明:
value:表示對(duì)象名,可省略
description:描述,可省略
5、@ApiModelProperty的使用說(shuō)明
修飾對(duì)象類(lèi)中的屬性,對(duì)屬性進(jìn)行說(shuō)明,屬性說(shuō)明:
- value:字段說(shuō)明
- name:重寫(xiě)屬性名字
- dataType:重寫(xiě)屬性類(lèi)型
- required:是否必填
- example:舉例說(shuō)明
- hidden:是否隱藏
@ApiModel(value="user對(duì)象",description="用戶(hù)對(duì)象user")
public class User implements Serializable{
private static final long serialVersionUID = 1L;
@ApiModelProperty(value="用戶(hù)名",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("更改用戶(hù)信息")
@PostMapping("/updateUserInfo")
public int updateUserInfo(@RequestBody @ApiParam(name="用戶(hù)對(duì)象",value="傳入json格式",required=true) User user){
int num = userService.updateUserInfo(user);
return num;
}
效果圖:

6、@ApiIgnore的使用說(shuō)明
修飾類(lèi)、方法、參數(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ù)類(lèi)型
- paramType:參數(shù)類(lèi)型
- example:舉例說(shuō)明
@ApiOperation("查詢(xún)測(cè)試")
@GetMapping("select")
//@ApiImplicitParam(name="name",value="用戶(hù)名",dataType="String", paramType = "query")
@ApiImplicitParams({
@ApiImplicitParam(name="name",value="用戶(hù)名",dataType="string", paramType = "query",example="xingguo"),
@ApiImplicitParam(name="id",value="用戶(hù)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)文章
關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié)
這篇文章主要介紹了關(guān)于Java鎖性能提高(鎖升級(jí))機(jī)制的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Springboot主程序類(lèi)注解配置過(guò)程圖解
這篇文章主要介紹了Springboot主程序類(lèi)注解配置過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
spring-boot整合Micrometer+Prometheus的詳細(xì)過(guò)程
這篇文章主要介紹了springboot整合Micrometer+Prometheus的詳細(xì)過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-05-05
Java詳解IO流創(chuàng)建讀取與寫(xiě)入操作
這篇文章主要介紹了Java IO流,同時(shí)也介紹了流中的一些相關(guān)的內(nèi)容,并且通過(guò)大量的案例供大家理解。最后通過(guò)一些經(jīng)典的案例幫助大家對(duì)前面所學(xué)的知識(shí)做了一個(gè)綜合的應(yīng)用,需要的朋友可以參考一下2022-05-05
Spring 單元測(cè)試中如何進(jìn)行 mock的實(shí)現(xiàn)
這篇文章主要介紹了Spring 單元測(cè)試中如何進(jìn)行 mock的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
基于javaweb+jsp的游泳館會(huì)員管理系統(tǒng)(附源碼)
這篇文章主要介紹了基于javaweb+jsp的游泳館會(huì)員管理系統(tǒng),開(kāi)發(fā)工具eclipse/idea/myeclipse/sts等均可配置運(yùn)行,此源代碼社和課程設(shè)計(jì),大作業(yè)及畢業(yè)設(shè)計(jì)項(xiàng)目,需要的朋友可以參考下2022-04-04
Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

