fno*_*sen 10 maven kotlin ktor openapi-generator
当尝试从 openapi 为 ktor 生成服务器端存根时,对我来说输出看起来并不是很实用。
我在 Github 上设置了一个示例项目,可以在其中查看设置。因为我需要在我的实际项目中使用 maven,所以我在这里也使用了它,但我想这对其他生成方法应该没有影响。
POM 的相关部分是这样的:
<build>
...
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>resources</id>
<phase>compile</phase>
<goals><goal>copy-resources</goal></goals>
<configuration>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
将生成的内容如下:
Paths.kt包含 ktor-locations 格式的路径路径的对象io.ktor.routing.Route来自 SpringBoot 和 Swagger,我有点困惑,因为如果您想创建一个实现真正业务逻辑的实际应用程序,这组文件并不是很有用。我本来希望一些默认实现包含我只需要实现/覆盖的单个方法。我意识到 ktor 没有内置 DI,所以我也希望必须以routing某种方式扩展我的应用程序中的存根。
但是,有了这组生成的代码,我真的只能使用数据模型和Paths.kt对象。我将拥有自己的应用程序和默认实现,我无法覆盖但需要自己完全重写。
所以我的问题是,如何设置一个合适的 ktor 应用程序实现 openapi 规范。我是否错过了什么,或者我真的只得到模型并Paths.kt与之合作?
编辑
为了更清楚:特别是对于我的应用程序的这些部分,我不高兴没有工具的支持:
fun Application.module(testing: Boolean = false) {
// other setup
routing {
// some other interfaces I may have
FooApiImpl()
}
}
Run Code Online (Sandbox Code Playgroud)
/**
* This Api Implementation has to be written completely manually. No help from the generated Code...
*/
@KtorExperimentalLocationsAPI
fun Route.FooApiImpl() {
get<Paths.getAllFoos> {
call.respond(listOf(
Foo( id = -1, bar = "hello"),
Foo( id = -2, bar = "world")
))
}
// the path gets named after the first method, which is weird in my eyes.
post<Paths.getAllFoos> {
val foo = call.receive<Foo>()
// fooService.save(foo)
call.respond(foo)
}
delete<Paths.deleteFooById> { path ->
val id = path.id
// fooService.deleteById(id)
call.respond(HttpStatusCode.NoContent)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1386 次 |
| 最近记录: |