扩展JDBI3设置的Kotlin等价物列出了在正式的Dropwizard文档中,没有@Bind和特定于Kotlin的映射魔术,JDBI无法获得自动参数绑定,如Kotlin对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>
...
}..I总是必须这样做:
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(...'发布于 2018-07-23 09:05:13
意见中提出的解决办法都是行之有效的。非常感谢!这个问题似乎是在一个不幸的过程中出现的:添加了正确的库,并在一个过程中修复了代码,这给人留下了不起作用的印象(我仍然不喜欢注释--狂热)。
任何可能在其中发现任何有用内容的人的摘要:
@BindBean使数据类确实可以作为参数工作。@Bind排除在争论之外显然有很长一段时间了,我只是没有注意到.@Bind("something") something: Thing而不是正确的(但仍然不必要的) @BindBean("something") something: Thing@Bind和@BindBean是有效的(至少在1001级的干净构建中是这样的)。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"
// ...
}https://stackoverflow.com/questions/51437036
复制相似问题