springboot 2.7.4 整合knife4j




Knife4j官网上的例子是基于SpringBoot 2.3.5 和 Knife4j 2.0.7 编写的。

按照官网写法,如果SpringBoot 版本是2.6以及以上就会报错。


SpringBoot 2.6 使用Spring Fox 接口统一api相关的文档接口,我们因此需要使用Knife4j 3.X 版本。


本文只使用了Knife4J的UI,文档框架直接使用Spring Fox


引入依赖

<!-- 文档 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-ui</artifactId>
<!--在引用时请在maven中央仓库搜索3.X最新版本号-->
<version>3.0.3</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>


创建配置类

package com.luo.usercenter.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;

/**
* 自定义swagger接口文档的配置
* @author lkx
*/
@Configuration
public class SwaggerConfig {

@Bean(value = "defaultApi2")
public Docket defaultApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 分组名称
.groupName("用户中心")
.select()
//这里一定要标注你控制器的位置
.apis(RequestHandlerSelectors.basePackage("com.luo.usercenter.controller"))
.paths(PathSelectors.any())
.build();
}

/**
* api信息
* @return api对象信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("用户中心")
.description("用户中心接口文档")
.termsOfServiceUrl("https://github.com/manneia/")
.contact(new Contact("lkx","https://github.com/manneia/","luokaixuan2001@163.com"))
.version("1.0")
.build();
}
}



配置静态资源


package com.luo.usercenter;

import com.luo.usercenter.config.WebMvcConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

/**
* @author lkx
*/
@SpringBootApplication
@EnableWebMvc
@MapperScan("com.luo.usercenter.mapper")
public class UserCenterApplication extends WebMvcConfig {

public static void main(String[] args) {
SpringApplication.run(UserCenterApplication.class, args);
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}


启动springboot项目 访问 localhost:IP/api/doc.html

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
归寻
作者分享
#提问# 项目在启动时mybatis-plus的多数据源块dynamic-datasource报错,但是配置文件是复用的主项目的, 报错信息为context with path [/bw_yto] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource ### The error may exist in com/baiwang/bwyto/ytjx/mapper/InvoiceCodeRelationMapper.java (best guess) ### The error may involve com.baiwang.bwyto.ytjx.mapper.InvoiceCodeRelationMapper.selectList ### The error occurred while executing a query ### Cause: com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource] with root cause com.baomidou.dynamic.datasource.exception.CannotFindDataSourceException: dynamic-datasource can not find primary datasource
3
#提问# 我现在接手的项目数据库使用的是orcāle数据库,然后在删除接口中,有两条脏数据,删除的时候无法删掉,我想知道可能导致这种情况的原因有什么,建表的话,没什么复杂的东西 -- auto-generated definition create table INVOICE_CODE_RELATION ( ID NUMBER(20) not null primary key, INVOICE_CODE_SYS VARCHAR2(30), ORG_NAME VARCHAR2(30), INVOICE_NO VARCHAR2(30), INVOICE_CODE VARCHAR2(30), SEND_FLAG NUMBER(2), MESSAGE VARCHAR2(50), CREATE_TIME DATE, UPDATE_TIME DATE ) /comment on table INVOICE_CODE_RELATION is '业务发票-发票关系对照表' /comment on column INVOICE_CODE_RELATION.ID is 'ID' /comment on column INVOICE_CODE_RELATION.INVOICE_CODE_SYS is '业务发票号码' /comment on column INVOICE_CODE_RELATION.ORG_NAME is '需方组织代码' /comment on column INVOICE_CODE_RELATION.INVOICE_NO is '发票号码' /comment on column INVOICE_CODE_RELATION.INVOICE_CODE is '发票代码' /comment on column INVOICE_CODE_RELATION.SEND_FLAG is '推送标记 0:未推送 1:推送成功 2:推送失败' /comment on column INVOICE_CODE_RELATION.MESSAGE is '推送描述' /comment on column INVOICE_CODE_RELATION.CREATE_TIME is '创建时间' /comment on column INVOICE_CODE_RELATION.UPDATE_TIME is '更新时间'/ 错误是数据库一直卡死, 原因猜测是有事务导致了表锁, 但是具体是什么不清楚
3
求助, OJ项目遇到了问题
5
实在不知道怎么解决了, logstash解压后启动出错了
0
Mybatis-plus完整sql打印
7
下载 APP