Skip to content
Server Plugin

XHttpMethodOverride

所需依赖项:io.ktor:ktor-server-method-override

代码示例: json-kotlinx-method-override

原生服务器Ktor 支持 Kotlin/Native,并允许您在无需额外运行时或虚拟机的情况下运行服务器。支持:✅

XHttpMethodOverride 插件提供了在 X-HTTP-Method-Override 标头中隧道传输 HTTP 动词的能力。 如果您的服务器 API 处理多种 HTTP 动词(GET、PUT、POST、DELETE 等),但客户端由于特定限制只能使用有限的一组动词(例如 GET 和 POST),该插件就会非常有用。 例如,如果客户端发送了一个将 X-Http-Method-Override 标头设置为 DELETE 的请求,Ktor 将使用 delete 路由处理程序来处理该请求。

添加依赖项 ​

要使用 XHttpMethodOverride,请将 ktor-server-method-override 工件添加到您的构建脚本中:

kotlin
implementation(ktorLibs.server.methodOverride)
上述代码片段使用 Ktor 版本目录来指定 Ktor 依赖项。
groovy
implementation ktorLibs.server.methodOverride
上述代码片段使用 Ktor 版本目录来指定 Ktor 依赖项。
xml
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-server-method-override-jvm</artifactId>
    <version>${ktor_version}</version>
</dependency>

安装 XHttpMethodOverride ​

要将 XHttpMethodOverride 插件安装到您的应用程序中,请在指定的模块模块允许您通过对路由进行分组来构建应用程序结构。中将其传递给 install 函数。 以下示例展示了如何安装 XHttpMethodOverride:

  • 在 embeddedServer() 函数调用中。
  • 在 Application 类显式定义的 module() 扩展函数中。
kotlin
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.plugins.methodoverride.*

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(XHttpMethodOverride)
        // ...
    }.start(wait = true)
}
kotlin
import io.ktor.server.application.*
import io.ktor.server.plugins.methodoverride.*
// ...
fun Application.module() {
    install(XHttpMethodOverride)
    // ...
}

配置 XHttpMethodOverride ​

默认情况下,XHttpMethodOverride 会检查 X-Http-Method-Override 标头以确定应由哪个路由来处理请求。 您可以使用 headerName 属性自定义标头名称。

示例 ​

以下 HTTP 请求使用了 POST 动词,并将 X-Http-Method-Override 标头设置为 DELETE:

http
POST http://0.0.0.0:8080/customer/3
X-Http-Method-Override: DELETE

要使用 delete 路由处理程序处理此类请求,您需要安装 XHttpMethodOverride:

kotlin
package com.example

import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.methodoverride.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import io.ktor.server.util.getValue

@Serializable
data class Customer(val id: Int, val firstName: String, val lastName: String)

fun Application.main() {
    val customerStorage = mutableListOf<Customer>()
    customerStorage.addAll(
        arrayOf(
            Customer(1, "Jane", "Smith"),
            Customer(2, "John", "Smith"),
            Customer(3, "Jet", "Brains")
        )
    )

    install(XHttpMethodOverride)
    install(ContentNegotiation) {
        json(Json)
    }
    routing {
        get("/customer/{id}") {
            val id: Int by call.parameters
            val customer: Customer = customerStorage.find { it.id == id }!!
            call.respond(customer)
        }

        delete("/customer/{id}") {
            val id: Int by call.parameters
            customerStorage.removeIf { it.id == id }
            call.respondText("Customer is removed", status = HttpStatusCode.NoContent)
        }
    }
}

您可以在此处找到完整的示例:json-kotlinx-method-override。