码迷,mamicode.com
首页 > 编程语言 > 详细

springboot2,gradle集成swagger2

时间:2020-04-17 21:47:28      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:esc   访问   ice   builder   png   autoconf   arp   yml   idt   

gradle文件增加

// swagger
    implementation "io.springfox:springfox-swagger2:2.9.2"
    implementation "io.springfox:springfox-swagger-ui:2.9.2"

  

添加配置类SwaggerConfig

package com.zuo.model.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 //添加swagger启用注解
//@Profile({"dev", "test"}) //方式一
//@ConditionalOnProperty(name = "swagger2.enable", havingValue = "true") //方式二
public class SwaggerConfig {

    //读取yml文件配置
    @Value("${swagger2.enable}")
    private boolean swagger2Enable;

    /**
     * .enable()  控制是否进行初始化
     * .select()  初始化并返回一个API选择构造器
     * .paths(PathSelectors.any())   设置路径筛选器
     * .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.xxx"))  添加路径选择条件
     * .build();    构建
     *
     * PathSelectors 类的方法:
     *  - Predicate<String> any():满足条件的路径,该断言总为true
     *  - Predicate<String> none():不满足条件的路径,该断言总为false
     *  - Predicate<String> regex(final String pathRegex):符合正则的路径
     *
     * RequestHandlerSelectors 类的方法:
     *  - Predicate<RequestHandler> any():返回包含所有满足条件的请求处理器的断言,该断言总为true
     *  - Predicate<RequestHandler> none():返回不满足条件的请求处理器的断言,该断言总为false
     *  - Predicate<RequestHandler> basePackage(final String basePackage):返回一个断言(Predicate),该断言包含所有匹配basePackage下所有类的请求路径的请求处理器
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swagger2Enable)  //方式三
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zuo.model.controller"))  // 注意修改此处的包名
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("项目模板集成系统")
                .description("API接口文档")
                .version("1.0.0")
                .build();
    }
}

技术图片

生产环境建议不要启用swagger

配置如下

技术图片

 

 技术图片

 

 访问swagger

http://localhost/swagger-ui.html

配置成功

 

springboot2,gradle集成swagger2

标签:esc   访问   ice   builder   png   autoconf   arp   yml   idt   

原文地址:https://www.cnblogs.com/zzuuoo666/p/12722502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!