springboot整合Swagger3.0(丝袜哥)

1.导包

        <!--丝袜哥 共三个包,这里用的3.0版本-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>    

3.0版本和之前的使用注解有些不一样后面会标注出来

2.配置到springboot中

@Configuration
//@EnableSwagger2 3.0之前版本用
@EnableOpenApi
public class Swagger3Config {
    @Bean
    public Docket docket(Environment environment){
        //设置要显示的Swagger的环境  当前是dev
        Profiles profiles = Profiles.of("dev");
        //获取环境 判断现在是什么环境  如果当前的环境和profiles 一样为TRUE
        boolean b = environment.acceptsProfiles(profiles);
        System.out.println("目标环境和使用环境对比结果为:" + b);
        return new Docket(DocumentationType.OAS_30)
                //.apiInfo(new ApiInfoBuilder().title("众筹项目").build());
                .apiInfo(apiInfo())
                .groupName("大萝卜组")//组名
                .enable(b)//是否启动丝袜哥
                .select()
                //any() 扫描全部
                //none() 不扫描
                //basePackage() 扫描指定包
                //withClassAnnotation() 扫描类上的注解 参数是一个注解的反射对象
                //withMethodAnnotation(GetMapping.class)
                .apis(RequestHandlerSelectors.basePackage("com.dlb.controller"))
                //扫描过滤 ,只扫描 ant("请求")
                //.paths(PathSelectors.ant("/a/*"))
                .build();
    }
    //配置swagger信息 需要apiInfo()
    public ApiInfo apiInfo() {
        Contact contact = new Contact("大萝北北", "http://www.xxx.com", "xxx@qq.com");
        return new ApiInfo("项目名"
                , "本项目由大萝卜打造,使用springboot框架搭建。向着小目标前进"
                , "1.0"
                , "urn:tos"
                , contact
                , "萝北贝的博客"
                , "https://www.cnblogs.com/9080dlb/"
                , new ArrayList());
    }
}

3.Swagger的使用

@Api(tags = "登录模块")//类注解
@RestController
@RequestMapping("/login")public class LoginController {

   @ApiOperation("重设密码")//方法注解
    @RequestMapping("/resetPwd")
    public RespEntity a(){
    .....   } }

4.访问地址

项目启动后

* 访问3.0网址:http://localhost/swagger-ui/index.html
* 访问2.0的:http://localhost/swagger-ui.html

 5.启动报错问题

Failed to start bean 'documentationPluginsBootstrapper
如果报这个错误说明丝袜哥和springboot版本(2.6)不兼容了
2.6.0开始使用基于PathPatternParser的路径匹配,而Springfox版本一直没有更新还是使用的AntPathMatcher导致了这个问题,要处理问题也很简单,修改yaml文件,
将SpringBoot路劲匹配模式修改为AntPathMatcher就可以了,
配置如下:spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

posted @ 2022-02-14 14:31  大萝卜萌萌哒  阅读(241)  评论(0编辑  收藏  举报