springboot整合knife4j全過程
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)行開啟
部分配置如下:
# 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.最后訪問Knife4j的文檔
地址:http://ip:port/doc.html即可查看文檔
注意的是:如果在配置文件中修改context-path信息,比如context-path: /imageanalyse,
那訪問地址就是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)的頁面上
@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)頁跳轉(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)頁跳轉(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)文章
利用synchronized實(shí)現(xiàn)線程同步的案例講解
這篇文章主要介紹了利用synchronized實(shí)現(xiàn)線程同步的案例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Spring使用@Autowired注解靜態(tài)實(shí)例對(duì)象方式
這篇文章主要介紹了Spring使用@Autowired注解靜態(tài)實(shí)例對(duì)象方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
使用Spring AOP監(jiān)控指定方法執(zhí)行時(shí)間的代碼詳解
這篇文章主要介紹了使用Spring AOP監(jiān)控指定方法執(zhí)行時(shí)間,文中通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
SpringBoot集成MD2File實(shí)現(xiàn)Markdown轉(zhuǎn)PDF的示例代碼
MD2File是一種與Markdown文件格式相關(guān)的工具或庫,主要用于將Markdown格式的文本轉(zhuǎn)換為其他文件類型,Markdown是一種輕量級(jí)標(biāo)記語言,廣泛用于編寫易于閱讀和書寫的結(jié)構(gòu)化文檔,本文給大家講解了SpringBoot集成MD2File實(shí)現(xiàn)Markdown轉(zhuǎn)PDF的示例,需要的朋友可以參考下2025-06-06
SpringBoot2.1.3修改tomcat參數(shù)支持請(qǐng)求特殊符號(hào)問題
最近遇到一個(gè)問題,比如GET請(qǐng)求中,key,value中帶有特殊符號(hào),請(qǐng)求會(huì)報(bào)錯(cuò)。接下來通過本文給大家分享解決SpringBoot2.1.3修改tomcat參數(shù)支持請(qǐng)求特殊符號(hào) ,需要的朋友可以參考下2019-05-05
Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的真正理解
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

