Open API(Swagger) 공통 Header 생성

1312 단어 SwaggerJavaJava

모든 API에 공통으로 사용 되는 Header 값 생성

@OpenAPIDefinition
@Configuration
public class OpenApiConfig {
	@Bean
	public OpenAPI openAPI() {
		return new OPENAPI()
			.info(
				new Info().title("Welcome Open API")
					.version("v1.0")
					.description("open API test")
			)
	}
	
	@Bean
	public GroupedOpenApi publicAdminApi(){
		return GroupedOpenApi.builder()
			.pathToMatch("/admin/**")
			.packagedToScan("com.test.ky.admin")
			.group("Admin")
			.addOpenApiCustomiser(customGlobalOpenApiHeader())
			.build():
	}
	
	@Bean
	public GroupedOpenApi publicUserApi(){
		return GroupedOpenApi.builder()
			.pathToMatch("/user/**")
			.packagedToScan("com.test.ky.user")
			.group("User")
			.build():
	}
    
	@Bean
	public OpenApiCustomiser customGlobalOpenApiHeader() {
		Parameter userToken = new Parameter()
			.name("userToken")
			.in("header")
			.description("userToken 값")
			.required(true)
			.schma(new StringSchema());
            
		Parameter adminId = new Parameter()
			.name("adminId")
			.in("header")
			.description("관리자 ID")
			.required(true)
			.schma(new StringSchema());
			
		return openApi -> openApi.getPaths().values().forEach(
			operation -> operation
				.addParametersItem(userToken)
				.addParametersItem(adminId)
		);
	}
    
}

좋은 웹페이지 즐겨찾기