Springboot集成Swagger2

搞了将近一天的配置,参照网上N个配置,最后发现问题所在恨不得。。。。。

废话少说,pom.xml文件

        <springfox-swagger-ui.version>2.9.2</springfox-swagger-ui.version>
        <springfox-swagger2.version>2.9.2</springfox-swagger2.version>
        <springfox-staticdocs.version>2.6.1</springfox-staticdocs.version>

   <!-- swagger2.0依赖 -->

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-staticdocs</artifactId>
            <version>${springfox-staticdocs.version}</version>
            <scope>test</scope>
        </dependency>

依赖配置完成,创建SwaggerConfig类

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(RequestHandlerSelectors.basePackage("com.XXX"))
                .paths(PathSelectors.any())
                .build();
    }
    //以下尽情发挥填写
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("XXX")
                .contact("mrli")
                .version("1.0")
                .build();
    }
}

网上的博主都说在appliaction加@EnableSwagger2 这个注解,但是我的并没有加,只需要在config里加上就行了

然后访问

http://localhost:8080/doc.html

注意一点,配置shrio的童鞋一定要把路径给放开了,一定放开!一定放开! 说三遍

     /**
         * 以下代码片段是粘贴过来的,具体怎么实现的不知道
         * 反正能测试接口就行,感兴趣的小伙伴可以了解一下
         */
        filterChainDefinitionMap.put("/doc.html", "anon");
        filterChainDefinitionMap.put("/v2/api-docs", "anon");
        filterChainDefinitionMap.put("/swagger-ui.html/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");

接下来就可以访问到了在这里插入图片描述
因为shiro的原因,我一直访问的是他默认的页面,所配置的title等都不起作用,可算爬出来了。
本人萌新菜鸡一枚,请各位大佬多多指教

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值