Swagger是屬于比較推薦的一種。
Swagger的特點:1、在Spring Boot中配置非常簡單2、項目部署時,根據代碼自動生成文檔,通過html展示3、代碼改動后,項目重新部署時文檔會自動更新,無需手動更新文檔4、保證了代碼和文檔的一致性,不會出現不一致的情況5、可以直接在文檔界面上測試接口,無需再利用第三方來調試接口了幾個簡單的步驟,就可以在Spring Boot中配置Swagger2來實現API文檔自動生成:1、引入依賴: <!-- 文檔自動生成 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency>2、創建Swagger2配置類:@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("專題頁api文檔") .description("專題頁api文檔") .termsOfServiceUrl("http://terms-of-services.url") .version("1.0") .build(); }}通過@Configuration注解,讓Spring來加載該類配置;再通過@EnableSwagger2注解來啟用Swagger2。apis()可以指定有某個注解的方法需要生成API文檔,還可以指定掃描哪個包來生成文檔,比如:apis(RequestHandlerSelectors.basePackage("com.xjj.web.controller"))3、在Controller的方法中注解各種文檔描述:@RestController@RequestMapping("/topic/")public class TopicController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired TopicService topicService; @RequestMapping(value="getTopic", method = RequestMethod.GET) @ApiOperation(value="接口描述。。。", response = TopicResult.class) @ApiImplicitParams({ @ApiImplicitParam(name = "sn", value = "編號", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "token", value = "用戶token", required = true, dataType = "String", paramType = "query") }) public JsonResult getTopicBySn(HttpServletRequest request, @RequestParam String sn, @RequestParam String token){ JsonResult result = null; try { Topic topic = topicService.getTopic(sn); if(topic == null){ result = new JsonResult(ReturnCode.PARAMS_ERROR, "錯誤"); }else { result = new JsonResult(ReturnCode.SUCCESS, "成功", topic); } }catch (Exception e){ logger.error("getTopicBySn Exception", e); result = new JsonResult(ReturnCode.EXCEPTION); } return result; }}其中,response = TopicResult.class 表示返回值使用JsonResult的子類TopicResult來展示更詳細的內容;如果不指定,則使用返回值JsonResult來生成文檔。這兩個類也需要相應的注解(@ApiModel和@ApiModelProperty):@ApiModelpublic class JsonResult { @ApiModelProperty(value = "狀態碼", example="40001", required = true, position=-2) private String code; @ApiModelProperty(value = "返回消息", example="恭喜,成功!", required = true, position=-1) private String message; @ApiModelProperty(value = "具體數據") private Object data; //constructors, getters, setters 略...}@ApiModelpublic class TopicResult extends JsonResult { @ApiModelProperty(value = "專題詳情", required = true) private Topic data; //constructors, getters, setters 略...}對于里面的Topic類,也需要相應的@ApiModel和@ApiModelProperty注解(代碼略)。對于Controller中的參數注解,也可以直接放到參數列表中,比如:@RestController@RequestMapping("/apply/")public class ApplyController { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired ApplyService applyService; @RequestMapping(value="store-mgr-setup", method = RequestMethod.POST) @ApiOperation(value="接口描述") public JsonResult applyStoreMgrSetup(HttpServletRequest request, @ApiParam(value = "參數描述", required = true) @RequestBody DianApply dianApply){ JsonResult result = null; //其他代碼略... return result; }}4、API文檔查看和調試項目啟動后,訪問這個url就可以看到自動生成的API文檔了:http://localhost:8080/<project-name>/swagger-ui.html測試:填入相應的參數后,點擊下方“Try it out!”按鈕,即可完成了一次請求調用!
新聞熱點
疑難解答