SpringBoot中怎么整合Swagger2

发布时间:2021-07-30 13:44:42 作者:Leah
来源:亿速云 阅读:138

这期内容当中小编将会给大家带来有关SpringBoot中怎么整合Swagger2,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

第一步添加依赖

<!-- SwaggerUI 接口文档 http://{ip}:{prot}/swagger-ui.html -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>{version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>{version}</version>
</dependency>

第二步添加注解

@EnableSwagger2 //启动SwaggerUI,在启动类或Swagger配置类上添加该注解

第三步写配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {

/*
        //可以添加多个header或参数
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder
                //参数类型支持header, cookie, body, query etc
                .parameterType("header")
                //参数名
                .name("user-token")
                //默认值
                .defaultValue("t122222")
                .description("用户登录凭证")
                //指定参数值的类型
                .modelRef(new ModelRef("string"))
                //非必需,这里是全局配置
                .required(false).build();
        List<Parameter> aParameters = new ArrayList<>();
        aParameters.add(aParameterBuilder.build());
*/

        return new Docket(DocumentationType.SWAGGER_2)
//        return new Docket(DocumentationType.SPRING_WEB)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()// 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.any())// 对所有api进行监控
                // 不显示错误的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))// 错误error路径不监控
                .paths(Predicates.not(PathSelectors.regex("/actuator.*")))// 错误error路径不监控
                .paths(PathSelectors.regex("/.*"))// 对根下所有路径进行监控
                .paths(PathSelectors.any())   // 对所有路径进行监控
                // 自行修改为自己的包路径
//                .apis(RequestHandlerSelectors.basePackage("com.happyloves.zc.service.account.api"))
                .build()
//                .globalOperationParameters(aParameters)
                .enable(true);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API接口")
                .description("API接口文档")
                //服务条款网址
//                .termsOfServiceUrl("https://www.google.com")
                .version("1.0")
//                .contact(new Contact("啦啦啦", "url", "email"))
                .build();
    }
}

添加依赖

<!-- swagger-bootstrap-ui是 Swagger 的增强UI 实现,使文档更友好一点儿 http://{ip}:{prot}/doc.html -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>

上述就是小编为大家分享的SpringBoot中怎么整合Swagger2了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. SpringBoot2.0 整合 Swagger2 ,构建接口管理界面
  2. SpringBoot整合Swagger2代码实例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot swagger2

上一篇:SpringBoot如何使用protobuf格式的接口方式

下一篇:NumPy怎么实现多维数组中的线性代数

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》