最近项目搭建中遇到的一些问题,涉及到 mp 版本 swagger集成等
由于使用高版本springboot 导致集成遇到的一些问题
项目中使用springboot + maven的多module 方式构建
springboot版本 3.0.0-RC2
mp版本 3.4.0
dependency>com.baomidou mybatis-plus-boot-starter 3.4.0
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
通过多次找博文并未解决,后参考之前的项目,降级springboot版本到2.7.0 问题解决,应该是版本兼容问题;
io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0
Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.getPatterns()" because "this.condition" is null
由于springboot版本超过2.6之后,引入swagger确实会有问题,按照网上解决了半天还是不行;
最终如下:
1 pom 中的swagger依赖去掉,改为
com.github.xiaoymin knife4j-spring-boot-starter 3.0.2
2 配置类也要修改如下
@Configuration
@EnableSwagger2
public class SwaggerConfig {@Beanpublic 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*/@Beanpublic 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 不兼容问题**/@Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {List> allEndpoints = new ArrayList();Collection 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
增加如下配置类
@Configuration
public class BrowserAutoConfig {@Value("${server.servlet.context-path:/}")private String prefix;@EventListener({ApplicationReadyEvent.class})public void applicationReadyEvent() {// 这里需要注url:端口号后不加任何地址,跳转项目首页,比如index.html,否则404String 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文档页面了
页面效果如下:

经过此次项目搭建,深知各个版本之前是不兼容的,以后搭建的时候,一定要提前调研好个版本之前的匹配度,否则会造成很多莫名其妙的问题~~
下一篇:没有到达,离开这个世界