Google Cloud Endpoints不知道Objectify 4 Transaction中的Work类,导致ClassNotFoundException

时间:2013-01-28 13:59:53

标签: objectify google-cloud-endpoints

我创建了一个非常简单的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是微不足道的(为了简洁而排除了吸气剂和固定剂):

@Entity
public class TestEntity {

    @Id long id;
    String name;
}

我使用http://code.google.com/p/objectify-appengine/wiki/BestPractices#Use_Your_Own_Service

中的OfyService模式

但是,当我尝试生成Endpoints库时,我得到了Work的Cla​​ssNotFoundException。完整的堆栈跟踪如下:

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类中包含的其他Objectify类(例如Ref等)有类似的问题,我将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;
  }

}

3 个答案:

答案 0 :(得分:0)

您必须在端点的classpatch中包含objectify jar。这可以在endpoints.sh中找到(在linux中,不确定windows等价)。类路径的脚本中有一个条目 - 只需添加objectify jar文件的位置。

答案 1 :(得分:0)

我通过指向WEB-INF \ lib

的Build Path Libraries添加了Objectify jar

答案 2 :(得分:0)

maven设置可以找到详细信息here。如果您只是想快速起步,请[在此处](http://repo1.maven.org/maven2/com/googlecode/objectify/objectify/)并点击最新版本的Objectify(目前{{1然后下载4.0rc2/文件。

在Eclipse中:将objectify-*version*.jarguava.jar添加到objectify.jar。右键单击您的项目。转到属性 - &gt; Java构建路径 - &gt;库。单击“添加JAR ...”,导航到上述[Project]/war/WEB-INF/lib文件夹,单击“确定”(对每个jar执行一次)。