隨着軟件過程的不斷髮展,先後端分離開發模式被愈來愈的開發團隊使用,今天介紹下先後分離中必用的接口設計與調試工具-swagger2,前端人員根據swagger的描述,進行參數的傳遞;先後端聯調的時候,出現問題,首先使用swagger進行測試調用,定位問題,還能夠經過界面導出swagger2接口文檔,修改完善後做爲其餘系統調用說明文檔。前端
採用springboo+swagger2方式運行swagger。java
<!--swagger--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
@EnableSwagger2 //啓動swagger註解 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@Api(description = "測試管理類") public class TestController { @RequestMapping(value = "/getTestByUid", method = RequestMethod.GET) @ApiOperation(value = "查詢接口,獲取當前用戶數據", response = Object.class) public Object getTestByUid(@ApiParam(value = "類型,1公開,2新建,3收藏4,分享") @RequestParam String type) { try { Object result = testService.getTestByUid(type); return new ResponseEntity(result, HttpStatus.OK); } catch (Exception e) { logger.error("查詢接口報錯", e); return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } } }
通常使用這幾個標籤就夠用了,詳細API說明能夠百度一下,就不一一說明了。node
(1) @Api(description = "測試管理類")spring
該標籤用於類上,是對整個類的說明,例如:用戶管理模塊,會在swagger2界面上生成一個一級菜單,方法是它的二級菜單。後端
(2) @ApiOperation(value = "查詢接口,獲取當前用戶數據", response = Object.class)springboot
該標籤用於方法上,Value是描述,還能夠定義nodes進行進一步說明。app
(3)@ApiParam(value = "類型,1公開,2新建,3收藏4,分享")前後端分離
改標籤用於定義參數描述。工具
三個標籤分別是類->方法->參數測試
另外:點擊 」Try it out「按鈕就能夠調用後臺服務,進行先後端聯調測試及問題定位。