记录spring boot 整合shiro后 引入swagger2遇到的坑

本文记录了使用Swagger2 2.9.2遇到的问题,包括类型转换异常和模型展示错误,通过引入特定版本的swagger-annotations和调整依赖解决。最终建议使用2.8.0版本以确保稳定性和模型显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先说结论:不要使用 2.9.2版本的swagger2,使用更低版本的 2.8.0

过程

1、引入依赖

<!-- 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>

2、Swagger2配置



@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "rssoft.swaggerEnable", havingValue = "true")
@Scope
public class SwaggerConfig {

    final String rootPackagePath = "com.rssoft.ykzly.api.controllers";



    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("公开分组")
                .apiInfo(apiInfo())
                .select()
                //控制器的包路径
                .apis(RequestHandlerSelectors.basePackage(rootPackagePath + ".OpenGroup"))
                .paths(PathSelectors.regex("/_open_/.*"))
                .build();
    }

    @Bean
    public Docket controllerInternalGroup() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("用户token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("内部用户-接口分组")
                .apiInfo(apiInfo())
                .select()
                //控制器的包路径InternalGroup
                .apis(RequestHandlerSelectors.basePackage(rootPackagePath + ".InternalGroup"))
                .paths(PathSelectors.regex("/_internal_/.*"))
                .build()
                .globalOperationParameters(pars);
    }


    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("API文档")
                //描述
                .description("描述")
                .build();
    }
}

3、创建接口

  @ApiOperation(value = "添加使用者")
    @RequestMapping(value = "/consumer/create", method = RequestMethod.POST)
    public ApiResult consumerCreate(@RequestBody Consumer consumer,
                                    @ApiParam(name = "usableTimes", value = "使用次数", required = true) @RequestParam(value = "usableTimes", required = true, defaultValue = "") Integer usableTimes) {
        try {
            Device device = deviceService.getById(consumer.getDeviceNo());
            if (device.getAgentId() == null ) {
                return ApiResult.builder().status(false).message("请先绑定代理商!").build();
            }
            consumerService.addConsumer(consumer, usableTimes, device);
            return ApiResult.builder().status(true).message("添加成功!").build();
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.builder().status(false).message("添加失败!").errors(e.getMessage()).build();
        }
    }

4、在shiroConfiguration 文件把拦截放开

   filterChainDefinitionMap.put("/swagger-ui.html","anon");
        filterChainDefinitionMap.put("/swagger/**","anon");
        filterChainDefinitionMap.put("/swagger2/**","anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**","anon");
        filterChainDefinitionMap.put("/v2/**","anon");

5、启动项目后控制台报类型转换异常

 6、在某度后查询解决方法后发现是swagger2自身错误,添加依赖可以解决

  <!-- swagger -->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

        <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>

7、访问页面后控制台无报错

 8、准备运行该接口时页面报这个错误,而且看不到实体类的models

Errors

Hide

Resolver error at paths./_internal_/device/consumer/create.post.parameters.0.schema.$ref

Could not resolve reference because of:

 

9、多方查询无果后受到某个帖子老哥的启发 准备降低swagger2的 版本

 <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>

10、运行后控制台无异常而且实体类的models可正常查看,问题解决。

前前后后被这些个问题折腾了接近俩个小时,记录一下遇到的这个坑,希望可以帮到和我遇到同样问题的兄弟!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值