【发布时间】:2020-04-23 10:40:28
【问题描述】:
我使用 Objectify 6.0.5、Ktor 1.2.6、com.google.appengine:appengine:1.9.60
我通过教程https://github.com/objectify/objectify/wiki/Setup设置了web.xml,引导程序
web.xml
...
<listener>
<listener-class>com.group.Bootstrapper</listener-class>
</listener>
<filter>
<filter-name>ObjectifyFilter</filter-name>
<filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ObjectifyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
助推器
class Bootstrapper : ServletContextListener {
override fun contextInitialized(sce: ServletContextEvent?) {
println(" contextInitialized")
ObjectifyService.init(
ObjectifyFactory()
)
ObjectifyService.register(User::class.java)
}
override fun contextDestroyed(sce: ServletContextEvent?) {}
}
当我调用这个方法时
fun save(entity: T) {
ofy().save().entity(entity)
}
我发现错误
2020-01-05 17:55:09 ERROR Application:104 - Unhandled: GET - /test
java.lang.IllegalStateException: You must call ObjectifyService.init() before using Objectify
at com.google.common.base.Preconditions.checkState(Preconditions.java:511) ~[guava-28.1-android.jar:?]
at com.googlecode.objectify.ObjectifyService.factory(ObjectifyService.java:34) ~[objectify-6.0.5.jar:?]
at com.googlecode.objectify.ObjectifyService.ofy(ObjectifyService.java:51) ~[objectify-6.0.5.jar:?]
at com.group.dao.BaseDao.listAll(BaseDao.kt:15) ~[classes/:?]
at com.group.ApplicationKt$module$2$2.invokeSuspend(Application.kt:50) ~[classes/:?]
at com.group.ApplicationKt$module$2$2.invoke(Application.kt) ~[classes/:?]
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:268) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:141) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:161) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:27) ~[ktor-utils-jvm-1.2.6.jar:1.2.6]
at io.ktor.routing.Routing.executeResult(Routing.kt:147) ~[ktor-server-core-1.2.6.jar:1.2.6]
...
我使用“gcloud beta emulators datastore start”运行数据存储。它肯定会运行。可能是连接不上吧?
当我使用 appengineRun 运行应用程序时,数据存储区也会运行吗?
【问题讨论】:
-
听起来你的 contextInitialized() 方法没有被调用。
-
contextInitialized() 被调用。通过调试器检查
-
我使用 'gcloud beta emulators datastore start' 运行数据存储,它肯定可以运行。可能是连接不上吧?
-
可能是某种类加载器问题?查看
ObjectifyService.init()的代码,只有一行。不知何故,您在与init()不同的静态上下文中调用ofy()。 -
@stickfigure 你能告诉我怎么做吗?
标签: google-app-engine kotlin google-cloud-platform objectify ktor