目前,大多數公司都採用了先後端分離的開發模式,爲了解決先後端人員的溝通問題,後端人員在開發接口的時候會選擇使用swagger2來生成對應的接口文檔,swagger2提供了強大的頁面調試功能,這樣能夠有效解決先後端人員溝通難的問題。html
下面咱們使用SpringBoot結合swagger2生成Restful API文檔。java
一 搭建項目,引入依賴
新建一個spring-boot-swaager
的項目,引入swaager2的依賴,因爲swagger2的ui不是很美觀,這裏將使用開源的swagger-bootstrap-ui
作爲ui。git
引入依賴github
<!-- swaager2依賴 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swaager2ui --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
項目中配置swagger相關信息redis
@Configuration @EnableSwagger2 public class configuration { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.javatrip.swagger.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() // 標題 .title("某某項目接口文檔") // 描述 .description("swagger2接口文檔使用演示") // 版本 .version("1.0") // 許可證 .license("MIT") // 許可證地址 .licenseUrl("www.xx.com") // 服務端地址 .termsOfServiceUrl("https://www.cnblogs.com/zhixie/") // 聯繫信息 .contact(new Contact("java旅途","https://www.cnblogs.com/zhixie/","binzh303@163.com")) .build(); } }
訪問路徑,查看生成效果spring
文章中使用的這個ui,接口文檔地址爲ip:port/doc.html
,生成的文檔信息以下:數據庫
二 編寫Restful接口
新建實體類bootstrap
@ApiModel("用戶實體類") @Data @NoArgsConstructor @AllArgsConstructor public class Person { @ApiModelProperty("姓名") private String name; @ApiModelProperty(value = "年齡") private int age; }
新建Restful接口後端
@Api(tags = "用戶接口") [@RestController](https://my.oschina.net/u/4486326) @RequestMapping("person") public class PersonController { @ApiOperation(value = "獲取用戶列表",notes = "根據name獲取用戶列表") @ApiImplicitParams({ @ApiImplicitParam(name = "name",value = "用戶姓名",dataType = "String",required = true), @ApiImplicitParam(name = "age",value = "年齡",dataType = "int",required = true) }) @GetMapping("/{name}") public Person getPerson(@PathVariable("name") String name,@RequestParam int age){ return new Person(name,age); } @ApiOperation(value = "新增用戶",notes = "根據用戶實體類新增用戶") @ApiImplicitParam(name = "person",value = "用戶實體類",dataType = "Person",required = true) @PostMapping("add") public int addPerson(@RequestBody Person person){ if(StringUtils.isEmpty(person)){ return -1; } return 1; } @ApiOperation(value = "更新用戶信息",notes = "根據用戶實體更新用戶信息") @ApiImplicitParam(name = "person",value = "用戶實體類",dataType = "Person",required = true) @PutMapping("update") public int updatePerson(@RequestBody Person person){ if(StringUtils.isEmpty(person)){ return -1; } return 1; } @ApiOperation(value = "刪除用戶信息",notes = "根據用戶名刪除用戶信息") @ApiImplicitParam(name = "name",value = "用戶姓名",dataType = "String",required = true) @DeleteMapping("/{name}") public int deletePerson(@PathVariable(name = "name") String name){ if(StringUtils.isEmpty(name)){ return -1; } return 1; } }
三 swagger文檔簡介
我就直接用圖來表示了,這樣看着也更加直觀api
swagger2註解對應到文檔上的表現形式如上。swagger2支持在線調試,打開某個具體的接口,根據提示填寫對應的參數,點擊發送就可返回響應結果。
本文示例代碼已上傳至github,點個star
支持一下!
Spring Boot系列教程目錄
spring-boot-route(一)Controller接收參數的幾種方式
spring-boot-route(二)讀取配置文件的幾種方式
spring-boot-route(五)整合Swagger生成接口文檔
spring-boot-route(六)整合JApiDocs生成接口文檔
spring-boot-route(七)整合jdbcTemplate操做數據庫
spring-boot-route(八)整合mybatis操做數據庫
spring-boot-route(九)整合JPA操做數據庫
spring-boot-route(十一)數據庫配置信息加密
spring-boot-route(十二)整合redis作爲緩存
spring-boot-route(十三)整合RabbitMQ
spring-boot-route(十五)整合RocketMQ
spring-boot-route(十六)使用logback生產日誌文件
spring-boot-route(十七)使用aop記錄操做日誌
spring-boot-route(十八)spring-boot-adtuator監控應用
spring-boot-route(十九)spring-boot-admin監控服務
spring-boot-route(二十)Spring Task實現簡單定時任務
spring-boot-route(二十一)quartz實現動態定時任務
spring-boot-route(二十二)實現郵件發送功能
這個系列的文章都是工做中頻繁用到的知識,學完這個系列,應付平常開發綽綽有餘。若是還想了解其餘內容,掃面下方二維碼告訴我,我會進一步完善這個系列的文章!