前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot集成swagger

springboot集成swagger

作者头像
用户10125653
发布2022-11-10 21:58:30
2960
发布2022-11-10 21:58:30
举报
文章被收录于专栏:changechange

导入依赖

代码语言:javascript
复制
implementation 'com.github.xiaoymin:swagger-bootstrap-ui:1.9.6'
implementation 'com.spring4all:swagger-spring-boot-starter:1.9.1.RELEASE'

设置配置文件

代码语言:javascript
复制
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Import({BeanValidatorPluginsConfiguration.class})
@EnableConfigurationProperties({SwaggerProperties.class})
@ConditionalOnProperty(
    name = {"swagger.enabled"},
    matchIfMissing = true
)
public class SwaggerAutoConfig {
    public SwaggerAutoConfig() {
    }

    @Bean
    @Order(1)
    public Docket groupRestApi(SwaggerProperties swaggerProperties) {
        return (new Docket(DocumentationType.SWAGGER_2)).host(swaggerProperties.getHost()).apiInfo(this.groupApiInfo(swaggerProperties)).select().apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())).paths(PathSelectors.any()).build().securityContexts(Lists.newArrayList(new SecurityContext[]{this.securityContext()})).securitySchemes(Lists.newArrayList(new SecurityScheme[]{this.apiKey()}));
    }

    private ApiInfo groupApiInfo(SwaggerProperties swaggerProperties) {
        Contact contact = new Contact(swaggerProperties.getContact().getName(), "https://www.dqgs.fun", swaggerProperties.getContact().getEmail());
        return (new ApiInfoBuilder()).title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription()).termsOfServiceUrl("http://www.dqgs.fun/").version(swaggerProperties.getVersion()).contact(contact).build();
    }

    private ApiKey apiKey() {
        return new ApiKey("BearerToken", "Authorization", "header");
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(this.defaultAuth()).forPaths(PathSelectors.regex("/.*")).build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope};
        return Lists.newArrayList(new SecurityReference[]{new SecurityReference("BearerToken", authorizationScopes)});
    }
}

页面展示

image.png
image.png
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导入依赖
  • 设置配置文件
  • 页面展示
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com