线程“main”中的异常 java.lang.reflect.InvocationTargetException - Ktor Application

问题描述

以下是我在 heroku 上部署 ktor 应用程序(rest api)时遇到的异常。它在 heroku 本地服务器上运行良好,但在服务器上出现问题。我在这里使用 postgresql 数据库

here you can get the full source code ?? 

https://github.com/nameisjayant/Ktor-Todo-Api

> 2021-06-06T14:05:05.948824+00:00 app[web.1]: at
> com.codingwithjks.repository.DatabaseFactory.hikari(DatabaseFactory.kt:26)
>     2021-06-06T14:05:05.948898+00:00 app[web.1]: at com.codingwithjks.repository.DatabaseFactory.init(DatabaseFactory.kt:16)
>     2021-06-06T14:05:05.948953+00:00 app[web.1]: at com.codingwithjks.ApplicationKt.module(Application.kt:32)
>     2021-06-06T14:05:05.948994+00:00 app[web.1]: at com.codingwithjks.ApplicationKt.module$default(Application.kt:28)
>     2021-06-06T14:05:05.949068+00:00 app[web.1]: ... 24 more
>     2021-06-06T14:05:09.000000+00:00 app[api]: Build succeeded
>     2021-06-06T14:05:12.624192+00:00 heroku[web.1]: Process exited with status 1
>     2021-06-06T14:05:12.701399+00:00 heroku[web.1]: State changed from starting to crashed
>     2021-06-06T14:05:28.888453+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"
> host=todo-rest-api-23242.herokuapp.com
> request_id=36434e2c-f89b-4ff7-bfc3-89cc3b927211 fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=https
>     2021-06-06T14:05:29.482958+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico"
> host=todo-rest-api-23242.herokuapp.com
> request_id=c8c0bff6-91c6-4838-a5d7-3e83cca5f1dd fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=https
>     2021-06-06T14:12:03.000000+00:00 app[api]: Build started by user [email protected]
>     2021-06-06T14:13:02.973949+00:00 app[api]: Deploy d0fbe4c4 by user [email protected]
>     2021-06-06T14:13:02.973949+00:00 app[api]: Release v10 created by user [email protected]
>     2021-06-06T14:13:03.838082+00:00 heroku[web.1]: State changed from crashed to starting
>     2021-06-06T14:13:07.347769+00:00 heroku[web.1]: Starting process with command `./build/install/example/bin/example`
>     2021-06-06T14:13:09.742957+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will
> override them.
>     2021-06-06T14:13:09.743159+00:00 app[web.1]: bash: ./build/install/example/bin/example: No such file or directory
>     2021-06-06T14:13:09.802293+00:00 heroku[web.1]: Process exited with status 127
>     2021-06-06T14:13:09.886995+00:00 heroku[web.1]: State changed from starting to crashed
>     2021-06-06T14:13:30.000000+00:00 app[api]: Build succeeded
>     2021-06-06T14:13:40.561950+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"
> host=todo-rest-api-23242.herokuapp.com
> request_id=3064eaba-777b-4b6d-bd79-855d23dfe140 fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=https
>     2021-06-06T14:13:41.107491+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico"
> host=todo-rest-api-23242.herokuapp.com
> request_id=fe5be84d-6e79-4eda-aae4-c4b679ec2c62 fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=https
>     2021-06-06T14:14:03.555698+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"
> host=todo-rest-api-23242.herokuapp.com
> request_id=cc4d10b8-6650-400b-ad87-5f36b144ec44 fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=http
>     2021-06-06T14:14:04.222065+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico"
> host=todo-rest-api-23242.herokuapp.com
> request_id=c83df707-fe4f-47a1-a6c9-a39a23c83cc5 fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=http
>     2021-06-06T14:14:07.733808+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/"
> host=todo-rest-api-23242.herokuapp.com
> request_id=78efe78b-a180-4592-844e-eeb14e317ae2 fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=http
>     2021-06-06T14:14:08.394252+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico"
> host=todo-rest-api-23242.herokuapp.com
> request_id=2d2e989c-f695-4311-b3be-644381a2ea9a fwd="103.198.173.200"
> dyno= connect= service= status=503 bytes= protocol=http
>     2021-06-06T14:20:13.438814+00:00 heroku[web.1]: State changed from crashed to starting
>     2021-06-06T14:20:17.000000+00:00 app[api]: Build started by user [email protected]
>     2021-06-06T14:20:17.780691+00:00 heroku[web.1]: Starting process with command `./build/install/example/bin/example`
>     2021-06-06T14:20:21.761892+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will
> override them.
>     2021-06-06T14:20:21.762092+00:00 app[web.1]: bash: ./build/install/example/bin/example: No such file or directory
>     2021-06-06T14:20:21.840577+00:00 heroku[web.1]: Process exited with status 127
>     2021-06-06T14:20:21.910863+00:00 heroku[web.1]: State changed from starting to crashed
>     2021-06-06T14:21:14.241869+00:00 app[api]: Release v11 created by user [email protected]
>     2021-06-06T14:21:14.241869+00:00 app[api]: Deploy 8bf5ea75 by user [email protected]
>     2021-06-06T14:21:14.497150+00:00 heroku[web.1]: State changed from crashed to starting
>     2021-06-06T14:21:17.982363+00:00 heroku[web.1]: Starting process with command `java -jar build/libs/example-0.0.1-all.jar`
>     2021-06-06T14:21:20.424533+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will
> override them.
>     2021-06-06T14:21:20.428220+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2
> -Dfile.encoding=UTF-8
>     2021-06-06T14:21:20.989769+00:00 app[web.1]: 2021-06-06 14:21:20.986 [main] TRACE Application - {
>     2021-06-06T14:21:20.989792+00:00 app[web.1]: # application.conf @

jar:file:/app/build/libs/example-0.0.1-all.jar!/application.conf: 6 > 2021-06-06T14:21:20.989793+00:00 应用程序[web.1]:“应用程序”:{ > 2021-06-06T14:21:20.989794+00:00 app[web.1]:#application.conf@ jar:file:/app/build/libs/example-0.0.1-all.jar!/application.conf: 7 > 2021-06-06T14:21:20.989794+00:00 应用程序[web.1]:“模块”:[ > 2021-06-06T14:21:20.989795+00:00 app[web.1]:#application.conf@ jar:file:/app/build/libs/example-0.0.1-all.jar!/application.conf: 7 > 2021-06-06T14:21:20.989795+00:00 应用程序[web.1]:“com.codingwithjks.ApplicationKt.module” > 2021-06-06T14:21:20.989796+00:00 应用程序[web.1]: ] > 2021-06-06T14:21:20.989796+00:00 应用程序[web.1]: },> 2021-06-06T14:21:20.989797+00:00 app[web.1]:#application.conf@ jar:file:/app/build/libs/example-0.0.1-all.jar!/application.conf: 2 > 2021-06-06T14:21:20.989797+00:00 应用程序[web.1]:“部署”:{ > 2021-06-06T14:21:20.989797+00:00 app[web.1]:#env var PORT > 2021-06-06T14:21:20.989798+00:00 应用程序[web.1]:“端口”:“44723” > 2021-06-06T14:21:20.989798+00:00 应用程序[web.1]: },> 2021-06-06T14:21:20.989799+00:00 app[web.1]: # 内容隐藏 > 2021-06-06T14:21:20.989799+00:00 应用程序[web.1]:“安全”:“***” > 2021-06-06T14:21:20.989799+00:00 应用程序[web.1]:} > 2021-06-06T14:21:20.989800+00:00 应用程序[web.1]: > 2021-06-06T14:21:21.096772+00:00 app[web.1]: 2021-06-06 14:21:21.096 [main] INFO Application - Autoreload 被禁用,因为 > 开发模式已关闭。 > 2021-06-06T14:21:21.599262+00:00 app[web.1]:线程“main”java.lang.reflect.InvocationTargetException中的异常 > 2021-06-06T14:21:21.599515+00:00 app[web.1]: 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) > 2021-06-06T14:21:21.599646+00:00 app[web.1]: 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) > 2021-06-06T14:21:21.599701+00:00 app[web.1]: 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) > 2021-06-06T14:21:21.599802+00:00 app[web.1]: 在 java.lang.reflect.Method.invoke(Method.java:498) > 2021-06-06T14:21:21.599827+00:00 app[web.1]: 在 kotlin.reflect.jvm.internal.calls.CallerImpl$Method.callMethod(CallerImpl.kt:97) > 2021-06-06T14:21:21.599942+00:00 app[web.1]: 在 kotlin.reflect.jvm.internal.calls.CallerImpl$Method$Static.call(CallerImpl.kt:106) > 2021-06-06T14:21:21.600007+00:00 app[web.1]: 在 kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod$kotlin_reflection(KCallableImpl.kt:173) > 2021-06-06T14:21:21.600105+00:00 app[web.1]: 在 kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:112) > 2021-06-06T14:21:21.600196+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.callFunctionWithInjection(ApplicationEngineEnvironmentReloading.kt:406) > 2021-06-06T14:21:21.600282+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.executeModuleFunction(ApplicationEngineEnvironmentReloading.kt:360) > 2021-06-06T14:21:21.600378+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.access$executeModuleFunction(ApplicationEngineEnvironmentReloading.kt:35) > 2021-06-06T14:21:21.600475+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading$launchModuleByName$1.invoke(ApplicationEngineEnvironmentReloading.kt:311) > 2021-06-06T14:21:21.600551+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading$launchModuleByName$1.invoke(ApplicationEngineEnvironmentReloading.kt:35) > 2021-06-06T14:21:21.600660+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.avoidingDoubleStartupFor(ApplicationEngineEnvironmentReloading.kt:335) > 2021-06-06T14:21:21.600737+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.launchModuleByName(ApplicationEngineEnvironmentReloading.kt:310) > 2021-06-06T14:21:21.600827+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.access$launchModuleByName(ApplicationEngineEnvironmentReloading.kt:35) > 2021-06-06T14:21:21.600921+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading$instantiateAndConfigureApplication$1.invoke(ApplicationEngineEnvironmentReloading.kt:299) > 2021-06-06T14:21:21.601006+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading$instantiateAndConfigureApplication$1.invoke(ApplicationEngineEnvironmentReloading.kt:35) > 2021-06-06T14:21:21.601100+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.avoidingDoubleStartup(ApplicationEngineEnvironmentReloading.kt:317) > 2021-06-06T14:21:21.601183+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.instantiateAndConfigureApplication(ApplicationEngineEnvironmentReloading.kt:297) > 2021-06-06T14:21:21.601285+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.createApplication(ApplicationEngineEnvironmentReloading.kt:138) > 2021-06-06T14:21:21.601358+00:00 app[web.1]: 在 io.ktor.server.engine.ApplicationEngineEnvironmentReloading.start(ApplicationEngineEnvironmentReloading.kt:270) > 2021-06-06T14:21:21.601444+00:00 app[web.1]: 在 io.ktor.server.netty.NettyApplicationEngine.start(NettyApplicationEngine.kt:160) > 2021-06-06T14:21:21.601528+00:00 app[web.1]: 在 io.ktor.server.netty.EngineMain.main(EngineMain.kt:26) > 2021-06-06T14:21:21.601783+00:00 app[web.1]: 引起:java.lang.NullPointerException > 2021-06-06T14:21:21.601871+00:00 app[web.1]: 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:328) > 2021-06-06T14:21:21.602010+00:00 app[web.1]: 在 java.lang.classLoader.loadClass(ClassLoader.java:351) > 2021-06-06T14:21:21.602109+00:00 app[web.1]: 在 com.zaxxer.hikari.HikariConfig.attemptFromContextLoader(HikariConfig.java:970) > 2021-06-06T14:21:21.602190+00:00 app[web.1]: 在 com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:480) > 2021-06-06T14:21:21.602273+00:00 app[web.1]: 在 com.codingwithjks.repository.DatabaseFactory.hikari(DatabaseFactory.kt:26) > 2021-06-06T14:21:21.602361+00:00 app[web.1]: 在 com.codingwithjks.repository.DatabaseFactory.init(DatabaseFactory.kt:16) > 2021-06-06T14:21:21.602442+00:00 app[web.1]: 在 com.codingwithjks.ApplicationKt.module(Application.kt:32) > 2021-06-06T14:21:21.602529+00:00 app[web.1]: 在 com.codingwithjks.ApplicationKt.module$default(Application.kt:28) > 2021-06-06T14:21:21.602629+00:00 应用程序[web.1]: ... 24 更多 > 2021-06-06T14:21:27.903731+00:00 heroku[web.1]:进程以状态 1 退出 > 2021-06-06T14:21:27.974849+00:00 heroku[web.1]:状态从开始变为崩溃 > 2021-06-06T14:21:42.000000+00:00 app[api]:构建成功 > 2021-06-06T14:21:58.940235+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" > 主机=todo-rest-api-23242.herokuapp.com > request_id=631a1616-282e-42cc-9389-53795848093d fwd="103.198.173.200" > dyno=连接=服务=状态=503字节=协议=http > 2021-06-06T14:21:59.731558+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" > 主机=todo-rest-api-23242.herokuapp.com > request_id=17a44432-c880-4e11-a614-b0c8ec88033a fwd="103.198.173.200" > dyno=连接=服务=状态=503字节=协议=http > 2021-06-06T14:22:01.657491+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" > 主机=todo-rest-api-23242.herokuapp.com > request_id=e1545ce7-1056-499e-a480-0c31fec0de9d fwd="103.198.173.200" > dyno=连接=服务=状态=503字节=协议=http > 2021-06-06T14:22:02.340307+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" > 主机=todo-rest-api-23242.herokuapp.com > request_id=b4ee0d3b-3856-452d-abcf-ac3e10511d17 fwd="103.198.173.200" > dyno=connect=service=status=503 bytes=protocol=http

DatabaseFactory 类

package com.codingwithjks.repository

import com.codingwithjks.data.table.TodoTable
import com.codingwithjks.data.table.UserTable
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import kotlinx.coroutines.dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction

object DatabaseFactory {

    fun init(){
        Database.connect(hikari())
        transaction {
            SchemaUtils.create(UserTable)
            SchemaUtils.create(TodoTable)
        }
    }


    private fun hikari():HikariDataSource {
        val config = HikariConfig()
        config.driverClassName = System.getenv("JDBC_DRIVER_2")
        config.jdbcUrl = System.getenv("JDBC_DATABASE_URL_2")
        config.maximumPoolSize = 3
        config.isAutoCommit = true
        config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
        config.validate()

        return HikariDataSource(config)
    }

    suspend fun <T> dbQuery(block : ()->T):T =
        withContext(dispatchers.IO){
            transaction {
                block()
            }
        }
}

Application.kt 文件

package com.codingwithjks

import com.codingwithjks.auth.JwtService
import com.codingwithjks.auth.MySession
import com.codingwithjks.auth.hash
import com.codingwithjks.repository.DatabaseFactory
import com.codingwithjks.repository.TodoRepository
import com.codingwithjks.repository.UserRepository
import com.codingwithjks.routes.todos
import com.codingwithjks.routes.users
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.*
import io.ktor.http.*
import io.ktor.locations.*
import io.ktor.sessions.*
import io.ktor.auth.*
import io.ktor.auth.jwt.*
import io.ktor.gson.*
import io.ktor.features.*

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@OptIn(KtorExperimentalLocationsAPI::class)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    install(Locations) {
    }

    DatabaseFactory.init()
    val userDb = UserRepository()
    val todoDb = TodoRepository()
    val jwt = JwtService()
    val hashFunction = {s:String -> hash(s)}
    install(Sessions) {
        cookie<MySession>("MY_SESSION") {
            cookie.extensions["SameSite"] = "lax"
        }
    }

    install(Authentication) {
        jwt("jwt") {
            verifier(jwt.verifier)
            realm = "Todo Server"
            validate {
                val payload = it.payload
                val claim = payload.getClaim("id")
                val claimstring = claim.asInt()
                val user = userDb.findUser(claimstring)
                user
            }
        }
    }

    install(ContentNegotiation) {
        gson {
            setPrettyPrinting()
            disableHtmlEscaping()
        }
    }

    routing {
        get("/"){
            call.respondText("Hello world..")
        }
        users(userDb,todoDb,jwt,hashFunction)
        todos(todoDb,userDb)
    }
}

const val API_VERSION = "/v1"

请帮助我:)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)