關于knife4j的使用及配置
knife4j的使用配置
關鍵詞:swagger、springfox、swagger-bootstrap-ui
說明:一款更加美化,實用、方便生成漂亮離線文檔的接口文檔工具
第一步
引入依賴
?<dependency> ? ? ? ? ? ? <groupId>com.github.xiaoymin</groupId> ? ? ? ? ? ? <artifactId>knife4j-spring-boot-starter</artifactId> ? ? ? ? ? ? <version>2.0.8</version> ? ? ? ? ? ? <type>pom</type> ? ? ? ? </dependency>
第二步
配置swagger
@EnableSwagger2 @Component public class Swagger2 { ? ? @Bean ? ? public Docket createRestApi() { ? ? ? ? return new Docket(DocumentationType.SWAGGER_2) ? ? ? ? ? ? ? ? .groupName("default") ? ? ? ? ? ? ? ? .apiInfo(apiInfo()) ? ? ? ? ? ? ? ? .select() ? ? ? ? ? ? ? ? .apis(RequestHandlerSelectors.basePackage("xxx.controller")) ? ? ? ? ? ? ? ? .paths(PathSelectors.any()) ? ? ? ? ? ? ? ? .build(); ? ? } ? ? private ApiInfo apiInfo() { ? ? ? ? return new ApiInfoBuilder() ? ? ? ? ? ? ? ? .title("xxx接口說明") ? ? ? ? ? ? ? ? .description("OpenAPI規(guī)范接口描述") ? ? ? ? ? ? ? ? .version("v1.0") ? ? ? ? ? ? ? ? .contact(new Contact("xxx公司", "http://xxx.com/", "xxx@xxx.com")) ? ? ? ? ? ? ? ? .license("License") ? ? ? ? ? ? ? ? .licenseUrl("http://xxx.com/") ? ? ? ? ? ? ? ? .build(); ? ? } }
第三步
控制器加swagger-ui 注解(略)
第四步
啟動訪問
啟動springboot 項目
訪問:localhost:port/doc.html
knife4j 簡單使用
Swagger作為一款API文檔生成工具,雖然功能已經(jīng)很完善了,但是還是有些不足的地方。
knife4j簡介
knife4j完全遵循了springfox-swagger中的使用方式,并在此基礎上做了增強功能,如果用過Swagger,可以無縫切換到knife4j。
快速開始
1、 在pom.xml中增加knife4j的相關依賴
<!--整合Knife4j--> <dependency> ? ? <groupId>com.github.xiaoymin</groupId> ? ? <artifactId>knife4j-spring-boot-starter</artifactId> ? ? <version>2.0.4</version> </dependency>
2、 在Swagger2Config中增加一個@EnableKnife4j注解,該注解可以開啟knife4j的增強功能
/** ?* Swagger2API文檔的配置 ?*/ @Configuration @EnableSwagger2 @EnableKnife4j public class Swagger2Config { }
3、運行我們的SpringBoot應用,訪問API文檔地址即可查看:
http://localhost:8080/doc.html
功能特點
對比下Swagger,看看使用knife4j和它有啥不同之處
1、 返回結果集支持折疊,方便查看
2、 請求參數(shù)有JSON校驗功能
3、 knife4j支持導出離線文檔,方便發(fā)送給別人,支持Markdown格式
直接選擇文檔管理->離線文檔功能,然后選擇下載Markdown即可
4、忽略參數(shù)屬性
有時候我們創(chuàng)建和修改的接口會使用同一個對象作為請求參數(shù),但是我們創(chuàng)建的時候并不需要id,而修改的時候會需要id,此時我們可以忽略id這個屬性。
比如這里的創(chuàng)建商品接口,id、商品數(shù)量、商品評論數(shù)量都可以讓后臺接口生成無需傳遞,可以使用knife4j提供的@ApiOperationSupport注解來忽略這些屬性
@Api(tags = "PmsBrandController", description = "商品品牌管理") @Controller @RequestMapping("/brand") public class PmsBrandController { ? ? @Autowired ? ? private PmsBrandService brandService; ? ? private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class); ? ? @ApiOperation("添加品牌") ? ? @ApiOperationSupport(ignoreParameters = {"id","productCount","productCommentCount"}) ? ? @RequestMapping(value = "/create", method = RequestMethod.POST) ? ? @ResponseBody ? ? public CommonResult createBrand(@RequestBody PmsBrand pmsBrand) { ? ? ? ? CommonResult commonResult; ? ? ? ? int count = brandService.createBrand(pmsBrand); ? ? ? ? if (count == 1) { ? ? ? ? ? ? commonResult = CommonResult.success(pmsBrand); ? ? ? ? ? ? LOGGER.debug("createBrand success:{}", pmsBrand); ? ? ? ? } else { ? ? ? ? ? ? commonResult = CommonResult.failed("操作失敗"); ? ? ? ? ? ? LOGGER.debug("createBrand failed:{}", pmsBrand); ? ? ? ? } ? ? ? ? return commonResult; ? ? } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
基于Pinpoint對SpringCloud微服務項目實現(xiàn)全鏈路監(jiān)控的問題
這篇文章主要介紹了基于Pinpoint對SpringCloud微服務項目實現(xiàn)全鏈路監(jiān)控的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02Spring?Boot+Aop記錄用戶操作日志實戰(zhàn)記錄
在Spring框架中使用AOP配合自定義注解可以方便的實現(xiàn)用戶操作的監(jiān)控,下面這篇文章主要給大家介紹了關于Spring?Boot+Aop記錄用戶操作日志實戰(zhàn)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04Spring?Boot在Web應用中基于JdbcRealm安全驗證過程
這篇文章主要為大家介紹了Spring?Boot在Web應用中基于JdbcRealm安全驗證過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2023-02-02單點登錄的概念及SpringBoot實現(xiàn)單點登錄的操作方法
在本文中,我們將使用Spring Boot構建一個基本的單點登錄系統(tǒng),我們將介紹如何使用Spring Security和JSON Web Tokens(JWTs)來實現(xiàn)單點登錄功能,本文假設您已經(jīng)熟悉Spring Boot和Spring Security,感興趣的朋友一起看看吧2024-10-10