简体   繁体   English

在Kotlin之前

[英]before in Kotlin

I'm trying to set a before filter without a path to apply to all requests. 我正在尝试设置一个前置过滤器而没有应用于所有请求的路径。 In kotlin I'm trying to write: 在kotlin我试着写:

before { req, resp -> 
    // check auth then redirect if not authorized... 
}

The compiler says: 编译器说:

Cannot infer type for this parameter (req & resp). 无法推断此参数的类型(req&resp)。

How should I set a before filter in Kotlin without a route? 如何在没有路线的情况下在Kotlin中设置过滤器?

You can use the path * in Spark to match all paths, like this: 您可以使用Spark中的路径*来匹配所有路径,如下所示:

before("*", { req, res ->
    logger.info("${req.requestMethod().toUpperCase()} ${req.fullUri()} by ${req.ip()} (${req.userAgent()})")
})

// or:

before("*") { req, res -> // ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM