現在測試都提倡自動化測試,那我們作為后台的開發人員,也得進步下啊,以前用postman來測試后台接口,那個麻煩啊,一個字母輸錯就導致測試失敗,現在swagger的出現可謂是拯救了這些開發人員,便捷之處真的不是一點兩點。下面我們看下如何在微服務中將springboot與swagger來結合吧。
1、swagger是什么,這個我覺得凡是一個開發人員就應該知道度娘啊,絕對強大。
簡單說下,它的出現就是為了方便進行測試后台的restful形式的接口,實現動態的更新,當我們在后台的接口修改了后,swagger可以實現自動的更新,而不需要認為的維護這個接口進行測試。
2、springboot與swagger的集成:
第一步:jar包的引入:
關於jar包的引入出現了一個問題就是版本的問題,可能需要與你的編輯器或者jdk要匹配吧,試了幾個才最終成功導入jar。
第二步:swagger的配置啟動類編寫:
要使用swagger要進行一些配置,這個在界面的圖上是可以顯示的:類似於說明書:在這個類中我們會使用注解來進行啟動swagger:
具體配置如下:
package com.springboot.example; //swagger2的配置文件,在項目的啟動類的同級文件建立 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2 { //swagger2的配置文件,這里可以配置swagger2的一些基本的內容,比如掃描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //為當前包路徑 .apis(RequestHandlerSelectors.basePackage("com.springboot.example.Controller")) .paths(PathSelectors.any()) .build(); } //構建 api文檔的詳細信息函數,注意這里的注解引用的是哪個 private ApiInfo apiInfo() { return new ApiInfoBuilder() //頁面標題 .title("Spring Boot 測試使用 Swagger2 構建RESTful API") //創建人 .contact(new Contact("MarryFeng", "http://www.baidu.com", "")) //版本號 .version("1.0") //描述 .description("API 描述") .build(); } }
這里的坑是:所使用類的引入文件要注意到底是哪個,之前因為這個出錯了,
@Configuration @EnableSwagger2
這兩個注解,一個是swagger2的配置,一個是項目啟動的時候啟動swagger2.
具體什么意思看下代碼就知道了。
//為當前包路徑 .apis(RequestHandlerSelectors.basePackage("com.springboot.example.Controller"))
這個包指的是我們在哪些類中使用swagger2來測試。
第三步:使用swagger來進行模擬測試:
使用swagger2來進行測試接口主要是在哪些類中使用:這里我們依然選擇在controller層:
package com.springboot.example.Controller; import com.springboot.example.Service.StudentService; import com.springboot.example.entity.Student; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2017/9/13. */ @RestController @RequestMapping("api") @Api("swaggerDemoController相關的api") public class SwaggerDemoController { @Autowired private StudentService studentService; private static final Logger logger= LoggerFactory.getLogger(SwaggerDemoController.class); @ApiOperation(value = "根據id查詢學生信息", notes = "查詢數據庫中某個的學生信息") @ApiImplicitParam(name = "id", value = "學生ID", paramType = "path", required = true, dataType = "Integer") @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Student getStudent(@PathVariable int id) { logger.info("開始查詢某個學生信息"); return studentService.selectStudentById(id); } }
上面這些可以看下具體的注解是什么意思:
這樣swagger2與springboot就集成完畢了。
看下最終效果吧:
訪問路徑:
http://localhost:8080/swagger-ui.html
輸入id后,我們可以看到查詢結果:、
是不是很方便,我們不用像postman一樣來編寫入口,swagger2自動完成:
而且實時更新:
是不是很方便!
至此swagger2與springboot的集成完畢。