最近项目搭建中遇到的一些问题,涉及到 mp 版本 swagger集成等
文章目录
- 前言
- 一、引入mp启动报错
- 1 相关配置
- 2 报错 如下
- 3 解决方案
- 二、引入swagger
- 1 引入的pom
- 2 报错如下:
- 3 解决方案
- 三. 项目启动自动打开swagger页面
- 总结
前言
由于使用高版本springboot 导致集成遇到的一些问题
一、引入mp启动报错
1 相关配置
项目中使用springboot + maven的多module 方式构建
springboot版本 3.0.0-RC2
mp版本 3.4.0
dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
2 报错 如下
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
3 解决方案
通过多次找博文并未解决,后参考之前的项目,降级springboot版本到2.7.0 问题解决,应该是版本兼容问题;
二、引入swagger
1 引入的pom
<dependency><!--添加Swagger依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency><!--添加Swagger-UI依赖 -->
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2 报错如下:
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
3 解决方案
由于springboot版本超过2.6之后,引入swagger确实会有问题,按照网上解决了半天还是不行;
最终如下:
1 pom 中的swagger依赖去掉,改为
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
2 配置类也要修改如下
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.groupName("项目")
//是否开启 (true 开启 false隐藏。生产环境建议隐藏)
//.enable(false)
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
}
/**
* 配置基本信息
* @return
*/
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title("文档")
//文档描述
.description("项目")
//服务条款URL
.termsOfServiceUrl("http://localhost:8080/")
//版本号
.version("1.0.0")
//联系人
.contact(new Contact("项目组", "http://localhost", "xxxx@163.com"))
.build();
}
/**
* 增加如下配置可解决Spring Boot 与Swagger 3.0.0 不兼容问题
**/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}
3 yml中增加
spring:
# swagger 配置
mvc:
pathmatch:
matching-strategy: ant_path_matcher
访问地址为: http://localhost:port/doc.htm
三. 项目启动自动打开swagger页面
增加如下配置类
@Configuration
public class BrowserAutoConfig {
@Value("${server.servlet.context-path:/}")
private String prefix;
@EventListener({ApplicationReadyEvent.class})
public void applicationReadyEvent() {
// 这里需要注url:端口号后不加任何地址,跳转项目首页,比如index.html,否则404
String url = "http://localhost:8088/"+prefix+"/doc.html";
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
prefix这个是读取yml中可能配置的项目访问路径,可以保证即使更改,也能争取打开swagger页面
这样只要启动项目,就会自动打开当前项目的swagger文档页面了
页面效果如下:
总结
经过此次项目搭建,深知各个版本之前是不兼容的,以后搭建的时候,一定要提前调研好个版本之前的匹配度,否则会造成很多莫名其妙的问题~~