Background: In our (Kotlin) Springboot 3 project, we used the specification-first approach, and generated code from our openapi.yml specification using openapi-generator.
Now we want to publish the api docs, and display a Swagger UI page, with URLs following the usual conventions. When following the advice at Question 12.66 in the Springdoc FAQ, we end up with the yml file served as static content.
How can I achieve that the usual conventions are followed, i.e. the json spec is available at http://localhost:8080/my-app/api/api-docs
, and the swagger UI at http://localhost:8080/my-app/api/swagger-ui.html
?
UPDATE:
As additional background, here is the snippet from build.gradle.kts
that generates the code for the API:
openApiGenerate {
inputSpec.set("$rootDir/src/main/resources/static/my-app/api/openapi.yml")
outputDir.set(outputDirPath.get().toString())
packageName.set(apiPackageName)
apiPackage.set("$apiPackageName.api")
modelPackage.set("$apiPackageName.model")
modelNameSuffix.set("Dto")
generatorName.set("kotlin-spring")
configOptions.set(
mapOf(
"useSpringBoot3" to "true",
"delegatePattern" to "true",
"interfaceOnly" to "false",
"dateLibrary" to "java8",
"useTags" to "true",
"enumPropertyNaming" to "UPPERCASE"
)
)
}
The generated code that corresponds to the openapi.yml
looks correct. The code generation is executed before the build, and everything is in a single project.
tasks.withType<KotlinCompile> {
dependsOn(tasks.openApiGenerate)
mustRunAfter(tasks.openApiGenerate)
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
Snippet with the used dependencies:
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-mustache")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.springdoc:springdoc-openapi-data-rest:1.6.15")
implementation("org.springdoc:springdoc-openapi-ui:1.6.15")
implementation("org.springdoc:springdoc-openapi-kotlin:1.6.15")
implementation("org.springframework.boot:spring-boot-starter-validation")
developmentOnly("org.springframework.boot:spring-boot-devtools")
/* further deps related to database and test */
}
Snippet from application.yml
:
springdoc:
api-docs:
path: /api-docs
groups:
enabled: true
swagger-ui:
url: /my-app/api/openapi.yml
server:
servlet:
context-path: '/my-app/api'