關(guān)于knife4j的使用及配置
knife4j的使用配置
關(guān)鍵詞:swagger、springfox、swagger-bootstrap-ui
說明:一款更加美化,實(shí)用、方便生成漂亮離線文檔的接口文檔工具
第一步
引入依賴
?<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 注解(略)
第四步
啟動(dòng)訪問
啟動(dòng)springboot 項(xiàng)目
訪問:localhost:port/doc.html
knife4j 簡(jiǎn)單使用
Swagger作為一款A(yù)PI文檔生成工具,雖然功能已經(jīng)很完善了,但是還是有些不足的地方。
knife4j簡(jiǎn)介
knife4j完全遵循了springfox-swagger中的使用方式,并在此基礎(chǔ)上做了增強(qiáng)功能,如果用過Swagger,可以無縫切換到knife4j。
快速開始
1、 在pom.xml中增加knife4j的相關(guān)依賴
<!--整合Knife4j--> <dependency> ? ? <groupId>com.github.xiaoymin</groupId> ? ? <artifactId>knife4j-spring-boot-starter</artifactId> ? ? <version>2.0.4</version> </dependency>
2、 在Swagger2Config中增加一個(gè)@EnableKnife4j注解,該注解可以開啟knife4j的增強(qiáng)功能
/**
?* Swagger2API文檔的配置
?*/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {
}3、運(yùn)行我們的SpringBoot應(yīng)用,訪問API文檔地址即可查看:
http://localhost:8080/doc.html
功能特點(diǎn)
對(duì)比下Swagger,看看使用knife4j和它有啥不同之處
1、 返回結(jié)果集支持折疊,方便查看
2、 請(qǐng)求參數(shù)有JSON校驗(yàn)功能
3、 knife4j支持導(dǎo)出離線文檔,方便發(fā)送給別人,支持Markdown格式
直接選擇文檔管理->離線文檔功能,然后選擇下載Markdown即可
4、忽略參數(shù)屬性
有時(shí)候我們創(chuàng)建和修改的接口會(huì)使用同一個(gè)對(duì)象作為請(qǐng)求參數(shù),但是我們創(chuàng)建的時(shí)候并不需要id,而修改的時(shí)候會(huì)需要id,此時(shí)我們可以忽略id這個(gè)屬性。
比如這里的創(chuàng)建商品接口,id、商品數(shù)量、商品評(píng)論數(shù)量都可以讓后臺(tái)接口生成無需傳遞,可以使用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;
? ? }
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Pinpoint對(duì)SpringCloud微服務(wù)項(xiàng)目實(shí)現(xiàn)全鏈路監(jiān)控的問題
這篇文章主要介紹了基于Pinpoint對(duì)SpringCloud微服務(wù)項(xiàng)目實(shí)現(xiàn)全鏈路監(jiān)控的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
Spring?Boot+Aop記錄用戶操作日志實(shí)戰(zhàn)記錄
在Spring框架中使用AOP配合自定義注解可以方便的實(shí)現(xiàn)用戶操作的監(jiān)控,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot+Aop記錄用戶操作日志實(shí)戰(zhàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Spring Security 安全框架應(yīng)用原理解析
這篇文章主要介紹了Spring Security 安全框架應(yīng)用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Spring?Boot在Web應(yīng)用中基于JdbcRealm安全驗(yàn)證過程
這篇文章主要為大家介紹了Spring?Boot在Web應(yīng)用中基于JdbcRealm安全驗(yàn)證過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-02-02
Java中的隱式參數(shù)和顯示參數(shù)實(shí)例詳解
這篇文章主要介紹了Java中的隱式參數(shù)和顯示參數(shù)是什么,另外還有兩個(gè)小例子幫助大家理解,需要的朋友可以參考下。2017-08-08
Java實(shí)現(xiàn)Excel與HTML互轉(zhuǎn)
Excel是一種電子表格格式,而HTM則是一種用于創(chuàng)建網(wǎng)頁的標(biāo)記語言,雖然兩者在用途上存在差異,但有時(shí)我們需要將數(shù)據(jù)從一種格式轉(zhuǎn)換為另一種格式,下面我們就來看看具體實(shí)現(xiàn)方法吧2025-01-01
maven將項(xiàng)目打包上傳到nexus私服的詳細(xì)教程
這篇文章主要介紹了maven將項(xiàng)目打包上傳到nexus私服,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-07-07
單點(diǎn)登錄的概念及SpringBoot實(shí)現(xiàn)單點(diǎn)登錄的操作方法
在本文中,我們將使用Spring Boot構(gòu)建一個(gè)基本的單點(diǎn)登錄系統(tǒng),我們將介紹如何使用Spring Security和JSON Web Tokens(JWTs)來實(shí)現(xiàn)單點(diǎn)登錄功能,本文假設(shè)您已經(jīng)熟悉Spring Boot和Spring Security,感興趣的朋友一起看看吧2024-10-10

