spring swagger ui

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#/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容