使用Docket.globalRequestParameters的话,每个 REST API 都会有这么一个 request header,但是我每次都要设置,有没有可以全局设置的方法?
1
qinxi 2021-04-26 11:42:47 +08:00
如果是 auth 类 header 会有个小锁
new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xx")) .paths(PathSelectors.any()) .build().securitySchemes(securitySchemes()) //.globalOperationParameters(globalOperationParameters()) .securityContexts(securityContexts()); private List<ApiKey> securitySchemes() { ArrayList<ApiKey> list = new ArrayList<>(); list.add(new ApiKey("Authorization", "Authorization", "header")); return list; } private List<SecurityContext> securityContexts() { return Collections.singletonList( SecurityContext.builder() .securityReferences(defaultAuth()) .forPaths(PathSelectors.any()) .build() ); } private List<SecurityReference> defaultAuth() { AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; authorizationScopes[0] = authorizationScope; return Collections.singletonList( new SecurityReference("Authorization", authorizationScopes)); } //如果是普通的 header new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xx")) .paths(PathSelectors.any()) .build() //.securitySchemes(securitySchemes()) .globalOperationParameters(globalOperationParameters()) ; private List<Parameter> globalOperationParameters() { ArrayList<Parameter> list = new ArrayList<>(); Parameter parameter = new ParameterBuilder().name("key").required(true) .description("").parameterType("header").defaultValue("") .allowMultiple(false) .modelRef(new ModelRef("string")).build(); list.add(parameter); return list; } |
3
JasonLaw OP @qinxi #1 globalOperationParameters 只能在代码写死默认值吗?不可以在页面上设置默认值吗?
|