若依框架集成knife4j

knife4j可以提供更为美观的API文档管理工具,更方便的处理前后端对接的接口问题

因为项目后台框架使用的是若依,这里基于若依背景进行操作。

1、引入knife4j包

在ruoyi-admin的pom文件中引入下面的包

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

2、配置SwaggerConfig配置

在ruoyi-admin目录下创建SwaggerConfig.java文件,内容如下

其中.apis(RequestHandlerSelectors.basePackage)后面的内容可以改为controller层的地址,或者使用RequestHandlerSelectors.any()扫描所有的包

package com.qingshuxz.web.controller.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qingshuxz.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("若依-Ruoyi API文档")
                .description("详细的接口文档说明")
                .version("1.0.0")
                .contact(new Contact("联系人", "https://ruoyi.vip/", "邮箱"))
                .build();
    }
}

3、修改前端项目框架

src\views\tool\swagger\index.vue修改url为process.env.VUE_APP_BASE_API + "/doc.html"

data() {
    return {
      url: process.env.VUE_APP_BASE_API + "/doc.html"
    };
  },

4、登陆若依管理系统

在系统工具中点击系统接口查看

或者直接浏览器访问步骤3里面的地址也可以

出现以下界面就代表设置成功了,里面相关功能可以自己再探索

5、controller层注解

在Controller类或方法上使用Swagger相关的注解(如@Api@ApiOperation@ApiParam等),为每个接口提供详细的描述信息,具体的含义和功能可以参照Swagger常用注解详解,主要就是说明类、方法的作用以及入参是否必填等

tip:在操作步骤4登陆后台管理界面中的系统接口,可以会有报错信息,可以在SecurityConfig.java的antMatchers中添加以下两个链接

"/swagger-resources", "/v2/api-docs"
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值