springboot运用swagger的兼容性坑 keisawaakira

按照mall商城的步骤插入swagger时,遇到了版本及兼容等问题,解决花了点时间,归纳如下:

@EnableSwagger2注释的问题

swagger版本在2.10.x段时,并不存在该注释,需要用@EnableSwagger2WebFlux进行替代

而到了3.x.x版本,又将@EnableSwagger2放了回来,并将先前的@EnableSwagger2WebFlux弃用

Mapper文件重复写入

照文章直接重新运行generator,会产生Mapper.xml文件中的方法重复,重复运行几次generator就重复多少次方法,会导致springboot跑不起来,产生如下错误image-20220815005709475

将mapper删除并重新运行generator一次,即可

CompilationUnit下不存在isJavaInterface方法

这是由于mybatis-generator-core的版本高于1.3.7所致,在1.4.0开始,并不存在这个方法

解决方法有如下两个:

一、回退至1.3.7

较为方便

二、用其他方法替代

原文中使用该方法的目的是为了只对model下文件进行操作,而非mapper的interface文件,通过下断点以及查看isJavaInterface的实现,将:

@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
    super.addJavaFileComment(compilationUnit);
    //只在model中添加swagger注解类的导入
    if(!compilationUnit.isJavaInterface()&&!compilationUnit.getType().getFullyQualifiedName().contains(EXAMPLE_SUFFIX)){
        compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
    }
}

替换为:

@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
    super.addJavaFileComment(compilationUnit);
    //只在model中添加swagger注解类的导入
    if(compilationUnit.getClass()!=Interface.class&&!compilationUnit.getType().getFullyQualifiedName().contains(EXAMPLE_SUFFIX)){
        compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
    }
}

即可

Swagger空指针异常

这也是兼容问题,需要在application.yml中添加如下:

spring:
	mvc:
        pathmatch:
            matching-strategy: ant_path_matcher

同时需要添加如下的Bean,双管齐下才行:

@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));
}

运行成功但是Swagger报404

依旧是兼容问题,目前只尝试了将Swagger改回2.9.2的方法,即可