springboot集成swagger2多模块中文配置详细步骤,解决集成mybatis或mybatis-plus无法正常使用问题
pom.xm里写入swagger依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
Swaggerconfig类,代码如下
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swaggerconfig {
@Value("${swagger2.enable}")
private boolean enable;
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("学习项目")
.description("学习项目测试工具")
.contact("Lawrence")
.version("1.0")
.build();
}
@Bean("模块一")
public Docket controller1Apis() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("模块一")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.regex("/controller1.*"))
.build()
.apiInfo(apiInfo())
.enable(enable);
}
@Bean("模块二")
public Docket controller2Apis() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("模块二")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.regex("/controller2.*"))
.build()
.apiInfo(apiInfo())
.enable(enable);
}
}
因为使用了中文,有一些坑需要在application.yml文件里配置
server:
port: 9527
tomcat:
uri-encoding: UTF-8
max-threads: 800
min-spare-threads: 30
max-connections: 10000
servlet:
context-path: /lean
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
swagger2:
enable: true
这样多模块配置就好了,访问项目路径/swagger-ui.html就ok,效果如下
注意,如果使用mybatis-plus的情况下会出现问题。原因是启动类@ComponentScan导致swagger无法扫描到接口,改为@MapperScan就可以解决了。
标签:
swagger2
, springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统