记录 maven + spring boot + shiro 整合 swagger2 访问 404 和 服务端报类型转换错误

一、添加 swagger 相关依赖配置

1、添加 maven 依赖

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

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.4</version>
        </dependency>

2、添加配置文件 Swagger2Config.java

@Profile({"dev","local"})//配置只有开发,测试环境才能访问接口文档
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("接口文档")
                .contact(new Contact("thanos","","cme.chang@vigekoo.com"))
                .version("1.0.0")
                .termsOfServiceUrl("")
                .build();
    }
}

3、在 ShiroConfig 添加允许访问

ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();

Map<String, String> filterMap = new LinkedHashMap<>(); 
       
filterMap.put("/doc.html/**", "anon");
filterMap.put("/swagger-ui.html/**", "anon");
filterMap.put("/swagger-resources/**", "anon");
filterMap.put("/v2/api-docs/**", "anon");
filterMap.put("/webjars/**", "anon");
filterMap.put("/swagger-resources/configuration/ui/**", "anon");
filterMap.put("/swagger-resources/configuration/security/**", "anon");

shiroFilter.setFilterChainDefinitionMap(filterMap);

二、配置添加完毕,访问文档

1、启动项目访问 http://localhost:8080/doc.html 或 http://localhost:8080/swagger-ui.html 都报404错误,页面不存在。
what ???

查看 swagger 的依赖结构,发现无论是 doc.html 还是 swagger-ui.html,都在META-INF/resources目录下,所以我们需要手动的将静态资源路径指向这里

修改swagger配置swagger2Config.java:

@Profile({"dev","local"})//配置只有开发,测试环境才能访问接口文档
@Configuration
@EnableSwagger2
public class Swagger2Config extends WebMvcConfigurationSupport {
    @Bean
    public Docket createDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("接口文档")
                .contact(new Contact("thanos","","cme.chang@vigekoo.com"))
                .version("1.0.0")
                .termsOfServiceUrl("")
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

修改 swagger 的配置类 swagger2Config.java 继承 WebMvcConfigurationSupport,实现 addResourceHandlers 方法,设置静态资源可访问。

设置完成后重启项目,就可以通过 http://localhost:8080/doc.html 或 http://localhost:8080/swagger-ui.html 正常访问了。

2、访问是可以正常访问了,但是每次访问都会报  java.lang.NumberFormatException: For input string: "". 的错误

这是因为: 
pom引入的io.springfox:springfox-swagger-ui:2.92版本的jar 包内置为io.swagger:swagger-models包为1.5.20版本。
1.5.20版本中判断example只判断是否为null,没有判断example为空字符串""的情况所以报错。
1.5.21版本新增了判断example是否为null和""。所以排除1.5.20包重新导入1.5.21包即可解决

修改依赖:
在 io.springfox:springfox-swagger-ui 依赖中排除 io.swagger:swagger-models 包,在新添加 io.swagger:swagger-models:1.5.21 依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>
 

重启服务 访问 http://localhost:8080/doc.html 或 http://localhost:8080/swagger-ui.html 就不会报错了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值