Kotlin 对 JDBI SqlObject 的支持给出了 UnsupportedOperationException

Kotlin support for JDBI SqlObject gives UnsupportedOperationException

扩展与列出的 Dropwizard JDBI3 设置等效的 Kotlin in the official Dropwizard documentation, I fail to get automatic parameter binding without @Bind and the Kotlin-specific mapping magic for JDBI to work as shown in Kotlin support for SqlObject。而不是这个...

data class Thing(val id: Int, val name: String,
                 val nullable: String?,
                 val nullableDefaultedNull: String? = null,
                 val nullableDefaultedNotNull: String? = "not null",
                 val defaulted: String = "default value")

interface ThingDao {
    @SqlUpdate("insert into something (id, name) values (:something.id, :something.name)")
    fun insert(something: Thing)

    @SqlQuery("select id, name from something")
    fun list(): List<Thing>

    ...
}

..我总是要做:

interface ThingDao {
    @SqlUpdate("insert into something (id, name) values (:id, :name)")
    fun insert(@Bind("id") id: Int?, @Bind("name") name: String)

    @SqlQuery("select id, name from something")
    fun list(): List<Thing>

    ...
}

Gradle 具有这些特定于 JDBI 的设置:

...
compile "io.dropwizard:dropwizard-jdbi3:1.3.5"
compile "org.jdbi:jdbi3-sqlobject:3.3.0"
compile "org.jdbi:jdbi3-postgres:3.3.0"
compile "org.jdbi:jdbi3-kotlin:3.3.0"
compile "org.jdbi:jdbi3-kotlin-sqlobject:3.3.0"
....

Dropwizard 应用程序具有以下 运行 配置:

override fun run(configuration: MyConfig, environment: Environment) {
    val factory = JdbiFactory()
    val jdbi = factory.build(environment, configuration.database, "postgresql")
    // This is said to install all available plugins and is thus redundant.
    // I have tried to include various combinations of the following in
    // some desperation. None work.
    jdbi.installPlugins()
    jdbi.installPlugin(SqlObjectPlugin())  // This...
    jdbi.installPlugin(KotlinPlugin())
    jdbi.installPlugin(KotlinSqlObjectPlugin())  // ..and this alone are said to do the job
    ...

否则,使用自定义 UUID 映射、Jackson Kotlin 数据对象映射等,一切似乎 运行 都很好。

使用:something.id的结果总是:

java.lang.UnsupportedOperationException: No argument factory registered for 'Thing(...'

评论中建议的解决方案均有效。非常感谢!问题似乎是在添加正确的库和修复代码的不幸序列中出现的,这个过程给人留下了无法正常工作的印象(我仍然不是注释狂热的粉丝)。

任何可能发现其中有用的人的摘要:

  1. 添加 @BindBean 使数据 类 确实可以用作参数。
  2. 省略参数 @Bind 显然工作了很长一段时间,我只是没有注意到...
  3. ..可能部分原因是我错误地尝试将它与数据 类 一起使用:@Bind("something") something: Thing 而不是正确的(但仍然不必要)@BindBean("something") something: Thing
  4. 鉴于此,删除 @Bind@BindBean 有效(至少在第 1001 个 gradle clean build 中)。
  5. 我删除了 compile "org.jdbi:jdbi3-sqlobject:$jdbi_version",这两种方式似乎对正常工作没有任何影响(据说 Dropwizard 已经引用了它)。

郑重声明,这些依赖项现在似乎对我有用:

buildscript {
  ext.kotlin_version = '1.2.51'
  ext.dropwizard_version = '1.3.5'
  ext.jdbi_version = '3.3.0'
  ...
}

...

dependencies {
  compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

  compile "io.dropwizard:dropwizard-core:$dropwizard_version"
  compile "io.dropwizard:dropwizard-jdbi3:$dropwizard_version"
  compile "io.dropwizard:dropwizard-auth:$dropwizard_version"
  compile "io.dropwizard:dropwizard-views-freemarker:$dropwizard_version"
  // This is to serve static content from resources/static
  compile "io.dropwizard:dropwizard-assets:$dropwizard_version"

  compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.9.4'
  compile 'com.fasterxml.jackson.module:jackson-modules-java8:2.9.4'

  // Removed as redundant
  // compile "org.jdbi:jdbi3-sqlobject:$jdbi_version"
  compile "org.jdbi:jdbi3-postgres:$jdbi_version"
  // So, these are still required as Dropwizard doesn't know Kotlin
  compile "org.jdbi:jdbi3-kotlin:$jdbi_version"
  compile "org.jdbi:jdbi3-kotlin-sqlobject:$jdbi_version"

  // Database
  compile 'org.postgresql:postgresql:42.1.4'

  // ...

  testCompile "io.dropwizard:dropwizard-testing:$dropwizard_version"

  // ...
}