我很想更好地了解 Ktor 如何处理静态内容的路由。我的静态文件夹(工作目录)中有以下层次结构:
- static
- index.html
- (some files)
- static
- css (directory)
- js (directory)
- (some files)
Run Code Online (Sandbox Code Playgroud)
我想为他们所有人服务。所以我直接在routing:
static {
defaultResource("index.html", "static")
resources("static")
}
Run Code Online (Sandbox Code Playgroud)
效果很好,但问题是它正在处理所有请求,包括我的小请求get:
get("/smoketest"){
call.respondText("smoke test!", ContentType.Text.Plain)
}
Run Code Online (Sandbox Code Playgroud)
一般来说,处理 Ktor 中的静态内容的最佳方法是什么?
谢谢
当尝试从 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) { …Run Code Online (Sandbox Code Playgroud) 在哪里可以找到将 gRPC 与 ktor 结合使用的示例。有可能吗,以前有人这样做过吗?我需要帮助。
我尝试遵循此示例: https: //github.com/renannprado/ktor-grpc-sample但我无法让它工作,并且示例可能不完整。
我正在从事Kotlin多平台项目。我使用Ktor客户端进行网络调用。我想根据一些不基于响应头的自定义逻辑缓存一些请求。
HttpCache提供的功能Ktor是响应头驱动的,我无法覆盖它,因为类的数量是Internal.
如何启用Ktor自定义缓存?
在启动 Ktor 应用程序期间,我有以下日志:
java.lang.UnsatisfiedLinkError: no netty_transport_native_epoll_x86_64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1871)
at java.lang.Runtime.loadLibrary0(Runtime.java:872)
at java.lang.System.loadLibrary(System.java:1124)
at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at io.netty.util.internal.NativeLibraryLoader$1.run(NativeLibraryLoader.java:369)
at java.security.AccessController.doPrivileged(Native Method)
at io.netty.util.internal.NativeLibraryLoader.loadLibraryByHelper(NativeLibraryLoader.java:361)
at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:339)
at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:136)
at io.netty.channel.epoll.Native.loadNativeLibrary(Native.java:231)
at io.netty.channel.epoll.Native.<clinit>(Native.java:58)
at io.netty.channel.epoll.Epoll.<clinit>(Epoll.java:39)
at io.ktor.server.netty.EventLoopGroupProxy$Companion.create(NettyApplicationEngine.kt:189)
at io.ktor.server.netty.NettyApplicationEngine.<init>(NettyApplicationEngine.kt:74)
at io.ktor.server.netty.EngineMain.main(EngineMain.kt:22)
22:56:07.760 [main] DEBUG io.netty.util.internal.NativeLibraryLoader - netty_transport_native_epoll_x86_64 cannot be loaded from java.library.path, now trying export to -Dio.netty.native.workdir: /tmp
java.lang.UnsatisfiedLinkError: no netty_transport_native_epoll_x86_64 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1871)
at java.lang.Runtime.loadLibrary0(Runtime.java:872)
at java.lang.System.loadLibrary(System.java:1124)
at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:349) …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 Ktor Netty 引擎创建我的应用程序
我在文档中搜索了当用户向我的服务器发送请求时处理发送电子邮件的任何功能,但什么也没找到。
post("/api/v1/auth") {
// TODO send email when request is sent!
}
Run Code Online (Sandbox Code Playgroud) 嘿,我试图用 ktor 获取客户端的 IP。
我用的方法
this.context.request.local.remoteHost
Run Code Online (Sandbox Code Playgroud)
(this.context是ApplicationCall的一个实例)
我怎样才能获得真实的IP而不是像“********.dip0.t-ipconnect.de”之类的东西
我正在使用Ktor 1.2.2,并且有一个 InputStream 对象,我想将其用作我接下来发出的 HttpClient 请求的主体。直到 Ktor 0.95 为止,这个InputStreamContent对象似乎就是这样做的,但它已在版本 1.0.0 中从 Ktor 中删除(遗憾的是无法弄清楚原因)。
我可以使用 ByteArrayContent 使其工作(请参见下面的代码),但我宁愿找到一个不需要将整个 InputStream 加载到内存中的解决方案......
ByteArrayContent(input.readAllBytes())
Run Code Online (Sandbox Code Playgroud)
此代码是一个简单的测试用例,模拟我想要实现的目标:
val file = File("c:\\tmp\\foo.pdf")
val inputStream = file.inputStream()
val client = HttpClient(CIO)
client.call(url) {
method = HttpMethod.Post
body = inputStream // TODO: Make this work :(
}
// [... other code that uses the response below]
Run Code Online (Sandbox Code Playgroud)
如果我遗漏了任何相关信息,请告诉我,
谢谢!
由于我是Android的新手,因此我正在构建一个应用程序,需要在该应用程序中更新Geo服务器上的图层数据。对于这种情况,我将与XML正文请求一起调用post方法。
我需要在请求正文中以xml格式发送数据,并且需要在xml中获取响应。为此,我尝试使用XmlSerializer代替JsonFeature,但出现错误,提示“ XmlSerializer不是同伴对象,需要在此处初始化”
val httpClient = HttpClient {
install(JsonFeature) {
serializer = KotlinxSerializer(Json.nonstrict)
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!!
我正在尝试在我的 Ktor 应用程序中安排一项任务,但是我无法在网上找到有关如何执行此操作的任何信息。有没有人有任何建议或以前能够做到这一点?
ktor ×10
kotlin ×8
java ×2
netty ×2
android ×1
grpc ×1
json ×1
ktor-client ×1
maven ×1
scheduling ×1