Ktor 服务器在 Postman 中响应 406 错误

问题描述 投票:0回答:1

我正在做一个后端服务器,在测试/注册路由时遇到一些问题。 我认为我已经正确配置了所有内容,但是当我尝试在 Postman 上执行 POST 方法时,它给了我 406 错误,这是我正在使用的链接“http://127.0.0.1:8080/v1/users/register” 我连接到了 PostGreSQL DB,在 Postman 上发出请求后,我什至检查了数据库,但那里什么也没有,在这个路由部分之前我已经使用 Ktor 创建了一个表。

这是我的路线文件




fun Route.userRoutes(db: repo, jwtService: JwtService, hashFunction: (String) -> String) {
    route("/v1/users") {
        post("/register") {
            try {
                val registerRequest = call.receive<RegisterRequest>()

                // Handle registration logic
                val user = User(registerRequest.email, hashFunction(registerRequest.password), registerRequest.name)
                db.addUser(user)
                call.respond(HttpStatusCode.OK, SimpleResponse(true, jwtService.generateToken(user)))

            } catch (e: Exception) {
                // Handle registration errors
                call.respond(HttpStatusCode.BadRequest, SimpleResponse(false, "Missing Some Fields or Registration Failed"))
            }
        }

报名课程

data class RegisterRequest(
    val email:String,
    val name:String,
    val password:String
)

简单响应类

package com.example.data.model

data class SimpleResponse(
    val success:Boolean,
    val message:String
)

当我尝试在 POSTMAN 上发送 POST 时,这就是我的 IntelliJ 控制台上显示的内容

2023-12-29 21:42:28.514 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.routing.Routing - Trace for [v1, users, register]
/, segment:0 -> SUCCESS @ /
  /session, segment:0 -> FAILURE "Selector didn't match" @ /session
  /json, segment:0 -> FAILURE "Selector didn't match" @ /json
  /, segment:0 -> SUCCESS @ /
    /(method:GET), segment:0 -> FAILURE "Selector didn't match" @ /(method:GET)
  /v1, segment:1 -> SUCCESS @ /v1
    /v1/users, segment:2 -> SUCCESS @ /v1/users
      /v1/users/register, segment:3 -> SUCCESS @ /v1/users/register
        /v1/users/register/(method:POST), segment:3 -> SUCCESS @ /v1/users/register/(method:POST)
      /v1/users/login, segment:2 -> FAILURE "Selector didn't match" @ /v1/users/login
Matched routes:
  "" -> "v1" -> "users" -> "register" -> "(method:POST)"
Route resolve result:
  SUCCESS @ /v1/users/register/(method:POST)
2023-12-30 21:04:26.349 [eventLoopGroupProxy-4-1] TRACE i.k.server.engine.DefaultTransform - No Default Transformations found for class io.ktor.utils.io.ByteBufferChannel and expected type TypeInfo(type=class com.example.data.model.RegisterRequest, reifiedType=class com.example.data.model.RegisterRequest, kotlinType=com.example.data.model.RegisterRequest) for call /v1/users/register
2023-12-30 21:04:26.375 [eventLoopGroupProxy-4-1] TRACE i.k.s.p.c.ContentNegotiation - No suitable content converter found for request type class com.example.data.model.RegisterRequest
2023-12-30 21:04:26.384 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.sessions.Sessions - Sending session data for /v1/users/register: MY_SESSION
2023-12-30 21:04:26.407 [eventLoopGroupProxy-4-1] TRACE i.k.s.p.c.ContentNegotiation - No suitable content converter found for response type class com.example.data.model.SimpleResponse and body SimpleResponse(success=false, message=Missing Some Fields)

请求的正文是这样的

{
    "name": "john",
    "email": "[email protected]",
    "password": "pass"
}
fun Application.configureSerialization() {

    install(ContentNegotiation) {

            Json {
                prettyPrint = true
                isLenient = true
                encodeDefaults = false
            }

    }

    routing {
        get("/json/gson") {
            call.respond(mapOf("hello" to "world"))
        }
    }
}

我已经检查了 Postman 上的标题选项卡,我有 Content-Type application/json 以及接受“/”

对Gradle的依赖我用的是latest.release

这只是一个简单的 Android 笔记应用程序。

问题似乎是因为 RegisterRequest 和 SimpleResponse 我认为。

这是我第一次使用 Ktor,如果我在某些方面落后了,很抱歉。谢谢您的宝贵时间

postman ktor
1个回答
0
投票

只需使用@serialized注释所有数据类 会起作用的。

© www.soinside.com 2019 - 2024. All rights reserved.