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("評(píng)估 app api")

????????????????????????????????.termsOfServiceUrl("http://團(tuán)隊(duì)鏈接")

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

????????????????????????????????.contact(new Contact("開(kāi)發(fā)者接口", "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#/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容