springboot整合knife4j全過(guò)程
1.首先引用Knife4j的starter
Maven坐標(biāo)如下:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>
2.以在配置文件中進(jìn)行開(kāi)啟
部分配置如下:
# springdoc-openapi項(xiàng)目配置
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
path: /v3/api-docs
group-configs:
- group: 'default'
paths-to-match: '/**'
packages-to-scan: com.xiaominfo.knife4j.demo.web
# knife4j的增強(qiáng)配置,不需要增強(qiáng)可以不配
knife4j:
enable: true
setting:
language: zh_cn
3.最后使用OpenAPI3的規(guī)范注解
注釋各個(gè)Spring的REST接口
示例代碼如下:
@RestController
@RequestMapping("body")
@Tag(name = "body參數(shù)")
public class BodyController {
@Operation(summary = "普通body請(qǐng)求")
@PostMapping("/body")
public ResponseEntity<FileResp> body(@RequestBody FileResp fileResp){
return ResponseEntity.ok(fileResp);
}
@Operation(summary = "普通body請(qǐng)求+Param+Header+Path")
@Parameters({
@Parameter(name = "id",description = "文件id",in = ParameterIn.PATH),
@Parameter(name = "token",description = "請(qǐng)求token",required = true,in = ParameterIn.HEADER),
@Parameter(name = "name",description = "文件名稱",required = true,in=ParameterIn.QUERY)
})
@PostMapping("/bodyParamHeaderPath/{id}")
public ResponseEntity<FileResp> bodyParamHeaderPath(@PathVariable("id") String id,@RequestHeader("token") String token, @RequestParam("name")String name,@RequestBody FileResp fileResp){
fileResp.setName(fileResp.getName()+",receiveName:"+name+",token:"+token+",pathID:"+id);
return ResponseEntity.ok(fileResp);
}
}
4.最后訪問(wèn)Knife4j的文檔
地址:http://ip:port/doc.html即可查看文檔
注意的是:如果在配置文件中修改context-path信息,比如context-path: /imageanalyse,
那訪問(wèn)地址就是http://ip:port//imageanalyse/doc.html
也可以建立一個(gè)默認(rèn)基礎(chǔ)Controller,當(dāng)請(qǐng)求路徑是/或者/swagger時(shí),路由默認(rèn)調(diào)轉(zhuǎn)到相對(duì)應(yīng)的頁(yè)面上
@RestController
@Tag(name = "默認(rèn)基礎(chǔ)Controller")
public class IndexController {
@Value("${server.servlet.context-path:#{null}}")
private String contextPath;
/**
* 自動(dòng)跳轉(zhuǎn)Knife4j-UI地址
*
* @param response
* @throws IOException
*/
@Operation(summary = "默認(rèn)頁(yè)跳轉(zhuǎn)knife4j-ui")
@GetMapping("/")
public void index(HttpServletResponse response) throws IOException {
String redirectPath="/doc.html";
if(contextPath !=null && contextPath.length()>0){
redirectPath = contextPath+redirectPath;
}
response.sendRedirect(redirectPath);
}
/**
* 自動(dòng)跳轉(zhuǎn)Swagger-UI地址
*
* @param response
* @throws IOException
*/
@Operation(summary = "默認(rèn)頁(yè)跳轉(zhuǎn)swagger-ui")
@GetMapping("/swagger")
public void swagger(HttpServletResponse response) throws IOException {
String redirectPath="/swagger-ui/index.html";
if(contextPath !=null && contextPath.length()>0){
redirectPath = contextPath+redirectPath;
}
response.sendRedirect(redirectPath);
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot配置多數(shù)據(jù)源并集成Druid和mybatis的操作
這篇文章主要介紹了springboot配置多數(shù)據(jù)源并集成Druid和mybatis的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
淺談java中math類中三種取整函數(shù)的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談java中math類中三種取整函數(shù)的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解
這篇文章主要介紹了Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解,CountDownLatch是一個(gè)同步工具類,它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下2023-12-12
String字符串轉(zhuǎn)BigDecimal時(shí),報(bào)NumberFormatException異常的解決
這篇文章主要介紹了String字符串轉(zhuǎn)BigDecimal時(shí),報(bào)NumberFormatException異常的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
簡(jiǎn)單理解遵循接口隔離原則的Java設(shè)計(jì)模式編程
這篇文章主要介紹了遵循接口隔離原則的Java設(shè)計(jì)模式編程,針對(duì)Java編程中interface接口方面的編寫(xiě)進(jìn)行約束,需要的朋友可以參考下2016-02-02
Springmvc自定義異常處理器實(shí)現(xiàn)流程解析
這篇文章主要介紹了Springmvc自定義異常處理器實(shí)現(xiàn)流程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

