我创建了一个非常简单的 Objectify/Endpoints 类。
import static mypackage.OfyService.ofy;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.googlecode.objectify.Work;
@Api(name = "testapi", version = "1")
public class TestEndpoint {
@ApiMethod(name = "test.insert")
public TestEntity insert(final TestEntity test) {
test.setName("test");
return ofy().transact(new Work<TestEntity>() {
public TestEntity run() {
ofy().save().entities(test).now();
return test;
}
});
}
}
TestEntity 是微不足道的(为简洁起见,排除了 getter 和 setter):
@Entity
public class TestEntity {
@Id long id;
String name;
}
我使用来自http://code.google.com/p/objectify-appengine/wiki/BestPractices#Use_Your_Own_Service的 OfyService 模式
但是,当我尝试生成 Endpoints 库时,我得到一个 ClassNotFoundException for Work。完整的堆栈跟踪如下:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gdt.eclipse.appengine.swarm.wizards.helpers.SwarmApiCreator.createSwarmApi(SwarmApiCreator.java:97)
at com.google.gdt.eclipse.appengine.swarm.wizards.helpers.SwarmServiceCreator.create(SwarmServiceCreator.java:196)
at com.google.gdt.eclipse.appengine.swarm.wizards.HandleGaeProjectChange.gaeProjectRebuilt(HandleGaeProjectChange.java:64)
at com.google.appengine.eclipse.core.properties.ui.GaeProjectChangeNotifier.build(GaeProjectChangeNotifier.java:77)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:239)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:292)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:295)
at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:351)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:374)
at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:143)
at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
Caused by: java.lang.NoClassDefFoundError: com/googlecode/objectify/Work
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethods(Class.java:1791)
at com.google.api.server.spi.MethodHierarchyReader.addServiceMethods(MethodHierarchyReader.java:174)
at com.google.api.server.spi.MethodHierarchyReader.readMethodHierarchyIfNecessary(MethodHierarchyReader.java:44)
at com.google.api.server.spi.MethodHierarchyReader.getEndpointOverrides(MethodHierarchyReader.java:99)
at com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader.readApiMethods(ApiConfigAnnotationReader.java:184)
at com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader.readEndpoint(ApiConfigAnnotationReader.java:82)
at com.google.api.server.spi.tools.AnnotationApiConfigGenerator.generateForService(AnnotationApiConfigGenerator.java:242)
at com.google.api.server.spi.tools.AnnotationApiConfigGenerator.generateConfigObjects(AnnotationApiConfigGenerator.java:227)
at com.google.api.server.spi.tools.AnnotationApiConfigGenerator.generateConfig(AnnotationApiConfigGenerator.java:176)
... 20 more
Caused by: java.lang.ClassNotFoundException: com.googlecode.objectify.Work
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 31 more
我在 Endpoint 类(例如 Ref 等)中包含的其他 Objectify 类没有类似的问题,并且我设置了 Eclipse (4.2) 项目以包含 Objectify 4.0b1 库。
如果我将 Endpoints 类更改为使用 VoidWork,我会得到完全相同的错误:
@Api(name = "testapi", version = "1")
public class TestEndpoint {
@ApiMethod(name = "test.insert")
public TestEntity insert(TestEntity test) {
final TestEntity newTest = new TestEntity();
newTest.setName(test.getName());
ofy().transact(new VoidWork() {
public void vrun() {
ofy().save().entities(newTest).now();
}
});
return newTest;
}
}