Springboot Swagger-ui 服务文档

1、简介

swagger是一款流行的API框架,用于生成服务文档,可视化、可调用、自定义规范,简化了开发过程,实时同步API文档的强大功能,相对于其他手写API文有极大优势

2、使用

  • 依赖 版本不同,自行选择
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>
  • 配置 1
@Configuration
@EnableSwagger2
public class Swagger {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .groupName("分组")
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.test.controller"))
        .paths(PathSelectors.any())
        .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
        .title("Spring boot Swagger-ui")
        .description("描述信息")
        .termsOfServiceUrl("http://www.baidu.com/")
        .version("1.0")
        .build();
    }
}
  • 配置 2
    @EnableSwagger2
    @Configuration
    public class Swagger {
        @Bean
        public Docket swaggerApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("/hello")
                    .genericModelSubstitutes(DeferredResult.class)
                    .useDefaultResponseMessages(false)
                    .forCodeGeneration(true)
                    .pathMapping("")// api测试请求地址,最终调用接口后会和paths拼接在一起
                    .select()
                    .paths(PathSelectors.regex("/.*"))// 过滤的接口
                    .build();
        }
    }

猜你喜欢

转载自blog.csdn.net/baidu_41241241/article/details/81262156