Springboot集成knife4j文档时,接口信息没有显示

我使用的 SpringBoot、knife4j 版本jar包如下所示:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/>
</parent>


<!-- 引入 knife4j组件, swagger-bootstrap-ui框架. -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.7</version>
</dependency>

下文中 Knife4jConfiguration.java 文件,其代码是我直接从 springboot-demo 项目中直接拷贝过来放到 MyBatis-SpringBoot 项目中。

springboot-demo项目Controller层包路径为: com.moon.controller ,MyBatis-SpringBoot 项目Controller层包路径为:com.moon.mybatis.controller 。 由于我忘记修改 Knife4jConfiguration.java 文件中 "RequestHandlerSelectors.basePackage(...)" 包路径,导致项目运行后,Swagger文档中没有接口信息。

Controller层包路径修改正确后,运行项目,Swagger文档中接口信息正常展示。

Knife4jConfiguration.java 文件

package com.moon.mybatis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义版 Knife4j 组件配置文件.
 */
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    @Bean(value = "defaultApi3")
    public Docket defaultApi2() {

        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(200).message("Success").build());
        responseMessageList.add(new ResponseMessageBuilder().code(400).message("参数错误").build());
        responseMessageList.add(new ResponseMessageBuilder().code(401).message("没有认证").build());
        responseMessageList.add(new ResponseMessageBuilder().code(403).message("没有访问权限").build());
        responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到资源").build());
        responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());

        ApiInfo apiInfoBuilder = new ApiInfoBuilder().title("Knife4j swagger-bootstrap-ui RESTful APIs")
                .description("Knife4j swagger-bootstrap-ui RESTful APIs ~~~~~")
                .termsOfServiceUrl("http://www.xxx.com")
                .contact(new Contact("张三", "http://www.moon.com", "xx@qq.com"))
                .version("1.0")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .globalResponseMessage(RequestMethod.GET, responseMessageList)
                .globalResponseMessage(RequestMethod.POST, responseMessageList)
                .globalResponseMessage(RequestMethod.PUT, responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                .apiInfo(apiInfoBuilder)
                .tags(new Tag("baidu", "百度接口API"), getTags())
                //分组名称
                .groupName("2.X版本")
                .enable(true)
                .select()
                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.moon.controller"))
                .apis(RequestHandlerSelectors.basePackage("com.moon.mybatis.controller"))
                .paths(PathSelectors.any())
                .build();

        return docket;
    }


    private Tag[] getTags() {

        return new Tag[] {
                new Tag("alibaba", "阿里巴巴API接口"),
                new Tag("tencent", "腾讯API接口")
        };
    }

}

程序运行后,Swagger接口文档信息展示如下所示:

Swagger 接口文档访问地址为:http://localhost:8080/doc.html#/home

本案例项目Demo地址为: Mybatis框架学习

如果大家觉得我的项目还行,可以加一个 Start ,感谢各位老铁了。

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值