springBoot结合swagger

介绍
简单来说就是后台api可视化,比如我以前写代码测我后台api可不可用,需要通过浏览器请求。现在直接在swagger提供的网址就可以测试了

pom

      <!--swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

配置类

public class SwaggerConfig {    
    @Bean
    public Docket testApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("name")
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// base,最终调用接口后会和paths拼接在一起
                .select()//函数返回一个 ApiSelectorBuilder 实例用来控制哪些接口暴露给Swagger2来展现
                .apis(RequestHandlerSelectors.basePackage("connet.controller"))//对api进行监控范围  
                .paths(PathSelectors.any())//过滤的接口   对所有路径进行监控
                .build()
                .apiInfo(testApiInfo());
    }
    private ApiInfo testApiInfo() {
        ApiInfo apiInfo = new ApiInfo(
                "大标题",//大标题
                "描述",//描述
                "版本",//版本
                "条款地址",//条款地址
                "联系",//联系
                "链接显示文字",//链接显示文字
                "网站链接/"//网站链接
        );
        return apiInfo;
    }

}

http://localhost:4847/swagger-ui.html#/
这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39168678/article/details/80182140