1

我创建了一个非常简单的 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;
  }

}
4

3 回答 3

0

我通过指向 WEB-INF\lib 的构建路径库添加了 Objectify jar

于 2013-08-06T22:15:41.833 回答
0

可以在此处找到有关 Maven 设置的详细信息。如果您只想快速启动并运行,请转到 [此处](http://repo1.maven.org/maven2/com/googlecode/objectify/objectify/] 并单击 Objectify 的最新版本(当前4.0rc2/),然后下载objectify-*version*.jar文件。

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

于 2013-10-27T01:39:45.077 回答
0

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

于 2013-02-22T17:13:27.907 回答