spring swagger ui

作者: feiai | 来源:发表于2018-04-08 11:34 被阅读0次
    spring4 @bean配置swagger 

    @Configuration

    @EnableSwagger2

    @EnableWebMvc

    public class SwaggerConfig {

        @Bean

        public Docket buildDocket() throws Exception {

                      return new Docket(DocumentationType.SWAGGER_2)

                                                    .apiInfo(buildApiInf())

                                                    .select().apis(RequestHandlerSelectors.basePackage("com.assist.assessment.app"))//controller路径

                    //.paths(PathSelectors.any())

                    .paths(paths())

                    .build()

                    .securitySchemes(newArrayList(apiKey()))

                    .securityContexts(newArrayList(securityContext()));

        }

    private Predicate paths() {

                return or(regex("/api.*"));

    }

    private ApiInfo buildApiInf() {

                return new ApiInfoBuilder()

                                    .title("评估 app api")

                                    .termsOfServiceUrl("http://团队链接")

                                    .description("swagger2 doc 接口文档描述")

                                    .contact(new Contact("开发者接口", "http://**.com", "248xx@qq.com"))

                                    .build();

    }

    private ApiKey apiKey() {

                                    return new ApiKey("clientSecret", "客户端密匙 api_key", "header");

    }

    private SecurityContext securityContext()throws Exception {

                                    return SecurityContext.builder()

                                    .securityReferences(defaultAuth())

                                    .forPaths(regex("/anyPath.*"))

                                    .build();

    }

    List<SecurityReference> defaultAuth() {

                            AuthorizationScope authorizationScope

                                    = new AuthorizationScope("global", "accessEverything");

                            AuthorizationScope[] authorizationScopes =new AuthorizationScope[1];

                            authorizationScopes[0] = authorizationScope;

                            return newArrayList(

                            new SecurityReference("clientSecret", authorizationScopes));

    }

    @Bean

     SecurityConfiguration security() {

                            return new SecurityConfiguration(

                                "test-app-client-id",

                                "test-app-client-secret",

                                "test-app-realm",

                                "test-app",

                                "apiKey",

                                ApiKeyVehicle.HEADER,

                                "api_key",

                                "," /*scope separator*/);

     }

    @Bean

    UiConfiguration uiConfig() {

                                return new UiConfiguration(

                                            "validatorUrl",// url

                                            "none",      // docExpansion          => none | list

                                            "alpha",      // apiSorter            => alpha

                                            "schema",    // defaultModelRendering => schema

                                            UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS,

                                            false,        // enableJsonEditor      => true | false

                                            true,        // showRequestHeaders    => true | false

                                60000L);      // requestTimeout => in milliseconds, defaults to null (uses jquery xh timeout)

        }

    }

    http://localhost:8080/swagger-ui.html#/

    相关文章

      网友评论

        本文标题:spring swagger ui

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