Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Ktor(Kotlin)管道的各个部分之间传递数据_Kotlin_Ktor - Fatal编程技术网

如何在Ktor(Kotlin)管道的各个部分之间传递数据

如何在Ktor(Kotlin)管道的各个部分之间传递数据,kotlin,ktor,Kotlin,Ktor,我正在构建一个API,并使用intercept(ApplicationCallPipeline.Call){}在每次路由执行之前运行一些逻辑。我需要将数据从intercept()方法传递到被调用的路由,然后 am在intercept()中使用call.attributes.put()设置数据,如下所示: val userKey=AttributeKey(“userK”) call.attributes.put(userKey,userData) 并使用call.attributes[userKe

我正在构建一个API,并使用
intercept(ApplicationCallPipeline.Call){}
在每次路由执行之前运行一些逻辑。我需要将数据从intercept()方法传递到被调用的路由,然后 am在intercept()中使用
call.attributes.put()
设置数据,如下所示:

val userKey=AttributeKey(“userK”)
call.attributes.put(userKey,userData)

并使用
call.attributes[userKey]
检索userData。 发生的情况是,
call.attributes[userKey]
只在我设置属性的intercept()方法中起作用。它在我需要它的路线上不起作用。 它把我甩了
java.lang.IllegalStateException:没有键属性key:userK的实例


我想知道我是否以正确的方式做事

以下是最简单的代码,再现了您所描述的内容:

class KtorTest {

    data class User(val name: String)

    private val userKey = AttributeKey<User>("userK")
    private val expected = "expected name"

    private val module = fun Application.() {
        install(Routing) {
            intercept(ApplicationCallPipeline.Call) {
                println("intercept")
                call.attributes.put(userKey, User(expected))
            }

            get {
                println("call")
                val user = call.attributes[userKey]
                call.respond(user.name)
            }

        }
    }

    @Test fun `pass data`() {
        withTestApplication(module) {
            handleRequest {}.response.content.shouldNotBeNull() shouldBeEqualTo expected
        }
    }

}
class-KtorTest{
数据类用户(val名称:字符串)
private val userKey=AttributeKey(“userK”)
private val expected=“expected name”
私有val模块=有趣的应用程序。(){
安装(路由){
拦截(ApplicationCallPipeline.Call){
println(“截取”)
call.attributes.put(userKey,用户(预期))
}
得到{
println(“调用”)
val user=call.attributes[userKey]
call.respond(user.name)
}
}
}
@测试乐趣`通过数据'(){
withTestApplication(模块){
HandlerRequest{}.response.content.shouldNotBeNull()应为预期值
}
}
}
我拦截调用,将用户放入属性中,最后在get请求中与用户一起响应。 考试通过了


您使用的ktor版本和引擎是什么?

以下是最简单的代码,再现了您所描述的内容:

class KtorTest {

    data class User(val name: String)

    private val userKey = AttributeKey<User>("userK")
    private val expected = "expected name"

    private val module = fun Application.() {
        install(Routing) {
            intercept(ApplicationCallPipeline.Call) {
                println("intercept")
                call.attributes.put(userKey, User(expected))
            }

            get {
                println("call")
                val user = call.attributes[userKey]
                call.respond(user.name)
            }

        }
    }

    @Test fun `pass data`() {
        withTestApplication(module) {
            handleRequest {}.response.content.shouldNotBeNull() shouldBeEqualTo expected
        }
    }

}
class-KtorTest{
数据类用户(val名称:字符串)
private val userKey=AttributeKey(“userK”)
private val expected=“expected name”
私有val模块=有趣的应用程序。(){
安装(路由){
拦截(ApplicationCallPipeline.Call){
println(“截取”)
call.attributes.put(userKey,用户(预期))
}
得到{
println(“调用”)
val user=call.attributes[userKey]
call.respond(user.name)
}
}
}
@测试乐趣`通过数据'(){
withTestApplication(模块){
HandlerRequest{}.response.content.shouldNotBeNull()应为预期值
}
}
}
我拦截调用,将用户放入属性中,最后在get请求中与用户一起响应。 考试通过了


您使用的是什么ktor版本以及哪个引擎?

我使用的是ktor 0.9.3和Netty,因此您必须显示更多代码,以便我们了解您在做什么。我的示例是有效的,因此您的代码可能存在一些问题。事实上,我定义了val userKey=AttributeKey(“userK”)两次。首先在intercept()中,然后在路由中,希望它们指向同一个键。谢谢@andreas-volkmann@AndreasVolkmann先生,很抱歉我在这里发表评论,我读了你的好文章。我对Kotlin和加密都是新手。直到上个月,我一直试图在kotlin找到AES-128加密的解决方案。但我没有找到任何解决办法。我的问题是,我在NettyThen中使用ktor 0.9.3,您必须显示更多代码,以便我们可以看到您正在做什么。我的示例是有效的,因此您的代码可能存在一些问题。事实上,我定义了val userKey=AttributeKey(“userK”)两次。首先在intercept()中,然后在路由中,希望它们指向同一个键。谢谢@andreas-volkmann@AndreasVolkmann先生,很抱歉我在这里发表评论,我读了你的好文章。我对Kotlin和加密都是新手。直到上个月,我一直试图在kotlin找到AES-128加密的解决方案。但我没有找到任何解决办法。我的问题在这里