springboot整合knife4j全過程
1.首先引用Knife4j的starter
Maven坐標如下:
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency>
2.以在配置文件中進行開啟
部分配置如下:
# springdoc-openapi項目配置 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的增強配置,不需要增強可以不配 knife4j: enable: true setting: language: zh_cn
3.最后使用OpenAPI3的規(guī)范注解
注釋各個Spring的REST接口
示例代碼如下:
@RestController @RequestMapping("body") @Tag(name = "body參數(shù)") public class BodyController { @Operation(summary = "普通body請求") @PostMapping("/body") public ResponseEntity<FileResp> body(@RequestBody FileResp fileResp){ return ResponseEntity.ok(fileResp); } @Operation(summary = "普通body請求+Param+Header+Path") @Parameters({ @Parameter(name = "id",description = "文件id",in = ParameterIn.PATH), @Parameter(name = "token",description = "請求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
也可以建立一個默認基礎Controller,當請求路徑是/或者/swagger時,路由默認調(diào)轉(zhuǎn)到相對應的頁面上
@RestController @Tag(name = "默認基礎Controller") public class IndexController { @Value("${server.servlet.context-path:#{null}}") private String contextPath; /** * 自動跳轉(zhuǎn)Knife4j-UI地址 * * @param response * @throws IOException */ @Operation(summary = "默認頁跳轉(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); } /** * 自動跳轉(zhuǎn)Swagger-UI地址 * * @param response * @throws IOException */ @Operation(summary = "默認頁跳轉(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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Springboot3集成Knife4j的步驟以及使用(最完整版)
- SpringBoot?Knife4j框架&Knife4j的顯示內(nèi)容的配置方式
- SpringBoot與knife4j的整合使用過程
- springboot讀取bootstrap配置及knife4j版本兼容性問題及解決
- springboot3整合knife4j詳細圖文教程(swagger增強)
- knife4j?整合?springboot的過程詳解
- SpringBoot中使用Knife4J的解決方案
- springboot集成swagger3與knife4j的詳細代碼
- Springboot中整合knife4j接口文檔的過程詳解
- knife4j+springboot3.4異常無法正確展示文檔
相關文章
SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫的配置方法
這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫的配置方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03解決SpringBoot使用@Value獲取不到y(tǒng)aml中配置值的問題
在最近的開發(fā)中遇到一個問題,使用@Value獲取yml文件中配置的屬性時始終獲取不到值,所以本文給大家詳細介紹了SpringBoot使用@Value獲取不到y(tǒng)aml中值的問題分析及解決方法,需要的朋友可以參考下2024-01-01JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺為例)
這篇文章主要介紹了JDK13.0.1安裝與環(huán)境變量的配置教程圖文詳解(Win10平臺為例),本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01關于IDEA中spring-cloud-starter-alibaba-nacos-discovery 無法引入問題
這篇文章主要介紹了關于IDEA中spring-cloud-starter-alibaba-nacos-discovery 無法引入問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02