美文网首页JHipsterjHipster
JHipster 纯后端怎么替换原生swagger并在swagg

JHipster 纯后端怎么替换原生swagger并在swagg

作者: brankLY | 来源:发表于2018-12-17 15:17 被阅读0次

    最近刚入手学习JHipster,项目要进行完全的前后端分离,在jhipster --skip-client生成开发环境后,在完全脱离前端的情况下,想要通过类似Spring-boot后端配置swagger来进行API接口展示和调用测试

    
    <dependency>
    
        <groupId>io.springfox</groupId>
    
        <artifactId>springfox-swagger-ui</artifactId>
    
        <version>2.9.2</version>
    
    </dependency>
    
    

    在如上添加maven依赖后,访问http://localhost:8080/swagger-ui.html可以看到

    但由于部分接口需要jwt token进行Authorization控制,而jhipster原生的swagger配置没有办法进行更新(Swagger configuration is located in core classes of JHipster),在检索不少问题后,最后采用下述方法解决。

    1.自己创建一个自己的SwaggerConfiguration用来替换原生的(该部分代码摘自https://www.jianshu.com/p/6e5ee9dd5a61)

    
    package xxxxxx;
    
    import org.springframework.context.annotation.Bean;
    
    import org.springframework.context.annotation.Configuration;
    
    import springfox.documentation.builders.PathSelectors;
    
    import springfox.documentation.builders.RequestHandlerSelectors;
    
    import springfox.documentation.service.ApiKey;
    
    import springfox.documentation.service.AuthorizationScope;
    
    import springfox.documentation.service.SecurityReference;
    
    import springfox.documentation.spi.DocumentationType;
    
    import springfox.documentation.spi.service.contexts.SecurityContext;
    
    import springfox.documentation.spring.web.plugins.Docket;
    
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.List;
    
    import static com.google.common.collect.Lists.newArrayList;
    
    @Configuration
    
    @EnableSwagger2
    
    public class SwaggerConfiguration {
    
    @Bean
    
        public Docketapi() {
    
    return new Docket(DocumentationType.SWAGGER_2).
    
    useDefaultResponseMessages(false)
    
    .select()
    
    .apis(RequestHandlerSelectors.any())
    
    .paths(PathSelectors.regex("^(?!auth).*$"))
    
    .build()
    
    .securitySchemes(securitySchemes())
    
    .securityContexts(securityContexts());
    
        }
    
    private ListsecuritySchemes() {
    
    return newArrayList(
    
    new ApiKey("Authorization", "Authorization", "header"));
    
        }
    
    private ListsecurityContexts() {
    
    return newArrayList(
    
    SecurityContext.builder()
    
    .securityReferences(defaultAuth())
    
    .forPaths(PathSelectors.regex("^(?!auth).*$"))
    
    .build()
    
    );
    
        }
    
    private ListdefaultAuth() {
    
    AuthorizationScope authorizationScope =new AuthorizationScope("global", "accessEverything");
    
            AuthorizationScope[] authorizationScopes =new AuthorizationScope[1];
    
            authorizationScopes[0] = authorizationScope;
    
            return newArrayList(
    
    new SecurityReference("Authorization", authorizationScopes));
    
        }
    
    }
    
    

    2. 在 /yourProject/src/main/resources 目录下创建META-INF文件夹,并创建spring.factories文件,配置您的swagger config。(摘自https://stackoverflow.com/questions/52147782/swagger-how-to-group-rest-methods-api-documentation/52164213#52164213

    image

    3.在启动类里exclude原生的swagger,来解决dockets冲突。

    
    @SpringBootApplication(exclude = {SwaggerAutoConfiguration.class})
    
    
    image

    4.替换后重启项目即可以加载自己的SwaggerConfiguration


    屏幕快照 2018-12-17 下午3.23.05.png

    相关文章

      网友评论

        本文标题:JHipster 纯后端怎么替换原生swagger并在swagg

        本文链接:https://www.haomeiwen.com/subject/luqckqtx.html