spring_boot整合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的规范注解,注释各个Spring的REST接口,示例代码如下:

@RestController
@RequestMapping("body")
@Tag(name = "body参数")
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时,路由默认调转到相对应的页面上

@RestController
@Tag(name = "默认基础Controller")
public class IndexController {


    @Value("${server.servlet.context-path:#{null}}")
    private String contextPath;

    /**
     * 自动跳转Knife4j-UI地址
     * 
     * @param response
     * @throws IOException
     */
    @Operation(summary = "默认页跳转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);
    }

    /**
     * 自动跳转Swagger-UI地址
     * 
     * @param response
     * @throws IOException
     */
    @Operation(summary = "默认页跳转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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值