Springboot集成swagger实现方式

Swagger 提供了一个全新的维护 API 文档的方式,有4大优点:

  • 自动生成文档:只需要少量的注解,Swagger 就可以根据代码自动生成 API 文档,很好的保证了文档的时效性。
  • 跨语言性,支持 40 多种语言。
  • Swagger UI 呈现出来的是一份可交互式的 API 文档,我们可以直接在文档页面尝试 API 的调用,省去了准备复杂的调用参数的过程。
  • 还可以将文档规范导入相关的工具(例如 SoapUI), 这些工具将会为我们自动地创建自动化测试。

如何实现swagger

一: pom文件加入依赖包



    io.springfox
    springfox-swagger2
    2.9.2



    io.springfox
    springfox-swagger-ui
    2.9.2

二:修改配置文件

1.application.properties 加入配置

#表示是否开启 Swagger,一般线上环境是关闭的
spring.swagger2.enabled=true

2.增加一个swagger配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value(value = "${spring.swagger2.enabled}")
    private Boolean swaggerEnabled;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(swaggerEnabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.swagger.boot"))//包名代表需要生成接口文档的目录包。
                .paths(PathSelectors.any())
                .build();
    }
     private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description(" Spring Boot")
                .version("1.0")
                .build();
    }
}

以上就是Springboot集成swagger实现方式的详细内容,更多关于Springboot集成swagger的资料请关注脚本之家其它相关文章!

你可能感兴趣的:(Springboot集成swagger实现方式)