關(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 注解(略)
第四步
啟動訪問
啟動springboot 項(xiàng)目
訪問:localhost:port/doc.html
knife4j 簡單使用
Swagger作為一款A(yù)PI文檔生成工具,雖然功能已經(jīng)很完善了,但是還是有些不足的地方。
knife4j簡介
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中增加一個@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)
對比下Swagger,看看使用knife4j和它有啥不同之處
1、 返回結(jié)果集支持折疊,方便查看
2、 請求參數(shù)有JSON校驗(yàn)功能
3、 knife4j支持導(dǎo)出離線文檔,方便發(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;
? ? }
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud基于zuul實(shí)現(xiàn)網(wǎng)關(guān)過程解析
這篇文章主要介紹了Spring Cloud基于zuul實(shí)現(xiàn)網(wǎng)關(guān)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
javaweb servlet中使用請求轉(zhuǎn)發(fā)亂碼的實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨avaweb servlet中使用請求轉(zhuǎn)發(fā)亂碼的實(shí)現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
Java實(shí)現(xiàn)按字節(jié)長度截取字符串
在Java中,由于字符串可能包含多字節(jié)字符,直接按字節(jié)長度截取可能會導(dǎo)致亂碼或截取不準(zhǔn)確的問題,下面我們就來看看幾種按字節(jié)長度截取字符串的方法吧2025-05-05
SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式
這篇文章主要介紹了SpringMVC實(shí)現(xiàn)RESTful風(fēng)格:@PathVariable注解的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring Boot Actuator未授權(quán)訪問漏洞的問題解決
Spring Boot Actuator 端點(diǎn)的未授權(quán)訪問漏洞是一個安全性問題,可能會導(dǎo)致未經(jīng)授權(quán)的用戶訪問敏感的應(yīng)用程序信息,本文就來介紹一下解決方法,感興趣的可以了解一下2023-09-09

