使用云端点

问题描述:

我使用物化和谷歌云端点来开发应用程序的后端在一次调用保存引用的对象(通过参考文献)。我有一个“卡车”类,有一个参考(使用参考)的“类别”类。它看起来像这样:使用云端点

@Entity 
@Cache 
public class Truck { 

    public @Id Long id; 
    public String city; 
    //... 
    @Load Ref<PlaceCategory> category; 

    public PlaceCategory getCategory(){ 
     return category.get(); 
    } 

    public void setCategory(PlaceCategory category){ 
     this.category = Ref.create(category); 
    } 
} 

自然,Category类别只是另一个具有它自己的ID的实体。现在

,建设我想能够在API - 在一个电话 - 插入包含类别对象Truck对象。在这种情况下,请求正文如下所示:

{ 
"category": { 
    "name": "Some category" 
}, 
"city": "Some city" 
} 

(它只是API explorer的一个副本)。 然而执行这一请求,我收到一个错误:

com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: No class 'entities.Category' was registered (through reference chain: entities.Truck[\"category\"])

当然,我已经注册类别对象OfyService作者:

public class OfyService { 

    static { 
     factory().register(PlaceCategory.class); 
    } 

    public static Objectify ofy() { 
     return ObjectifyService.ofy(); 
    } 

    public static ObjectifyFactory factory() { 
     return ObjectifyService.factory(); 
    } 
} 

我读过的文档两次,我仍然不知道哪来的问题。没有明确说过我不能像这样嵌入对象。经过2天的搜索和努力,我放弃了:)希望有人能帮助我理解。

+0

迄今发现的解决方案? – pjv 2015-03-11 22:47:10

下面的代码,非常接近你的,很适合我既开发服务器上,并在App Engine上。

我使用App Engine的1.8.9和物化4.0b3,也许您使用的是旧版本,并只需要升级?

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>hatanian.david.cloudplatform</groupId> 
    <artifactId>endpoints-objectify-ref</artifactId> 
    <packaging>war</packaging> 
    <version>1.0</version> 
    <name>Simple Endpoints API</name> 

    <properties> 
     <appengine.target.version>1.8.9</appengine.target.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <repositories> 
    </repositories> 

    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.mail</groupId> 
      <artifactId>mail</artifactId> 
      <version>1.4.5</version> 
     </dependency> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-api-1.0-sdk</artifactId> 
      <version>${appengine.target.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-endpoints</artifactId> 
      <version>${appengine.target.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <!--<dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-testing</artifactId> 
      <version>${appengine.target.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.google.appengine</groupId> 
      <artifactId>appengine-api-stubs</artifactId> 
      <version>${appengine.target.version}</version> 
      <scope>test</scope> 
     </dependency>--> 
     <dependency> 
      <groupId>com.googlecode.objectify</groupId> 
      <artifactId>objectify</artifactId> 
      <version>4.0b3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-jdk14</artifactId> 
      <version>1.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>log4j-over-slf4j</artifactId> 
      <version>1.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-io</groupId> 
      <artifactId>commons-io</artifactId> 
      <version>2.4</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.0</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.3</version> 
       <configuration> 
        <webXml>${project.build.directory}/generated-sources/appengine-endpoints/WEB-INF/web.xml</webXml> 
        <webResources> 
         <resource> 
          <!-- this is relative to the pom.xml directory --> 
          <directory>${project.build.directory}/generated-sources/appengine-endpoints</directory> 
          <!-- the list has a default value of ** --> 
          <includes> 
           <include>WEB-INF/*.discovery</include> 
           <include>WEB-INF/*.api</include> 
          </includes> 
         </resource> 
        </webResources> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>com.google.appengine</groupId> 
       <artifactId>appengine-maven-plugin</artifactId> 
       <version>${appengine.target.version}</version> 
       <configuration> 
        <enableJarClasses>false</enableJarClasses> 
       </configuration> 
       <executions> 
        <execution> 
         <goals> 
          <goal>endpoints_get_discovery_doc</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

Ofyservice:

package hatanian.david.simpleendpoint; 

import com.googlecode.objectify.Objectify; 
import com.googlecode.objectify.ObjectifyFactory; 
import com.googlecode.objectify.ObjectifyService; 

public class OfyService { 

    static { 
     factory().register(PlaceCategory.class); 
     factory().register(Truck.class); 
    } 

    public static Objectify ofy() { 
     return ObjectifyService.ofy(); 
    } 

    public static ObjectifyFactory factory() { 
     return ObjectifyService.factory(); 
    } 
} 

PlaceCategory:

@Entity 
public class PlaceCategory { 
    @Id 
    private String category; 

    public String getCategory() { 
     return category; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 
} 

卡车:

@Entity 
@Cache 
public class Truck { 

    public @Id Long id; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @Load 
    Ref<PlaceCategory> category; 

    public PlaceCategory getCategory(){ 
     return category.get(); 
    } 

    public void setCategory(PlaceCategory category){ 
     this.category = Ref.create(category); 
    } 
} 

和终点:

@Api(name = "simple", version = "v1", scopes = {EndPointsConstants.EMAIL_SCOPE}, clientIds = {EndPointsConstants.WEB_CLIENT_ID, com.google.api.server.spi.Constant.API_EXPLORER_CLIENT_ID}) 
public class SimpleEndpoint { 
    private Logger log = Logger.getLogger(SimpleEndpoint.class.getName()); 

    @ApiMethod(name = "create", httpMethod = "post") 
    public void create() { 
     PlaceCategory placeCategory = new PlaceCategory(); 
     placeCategory.setCategory("testcategory"); 
     OfyService.ofy().save().entity(placeCategory).now(); 

     Truck truck = new Truck(); 
     truck.setCategory(placeCategory); 
     truck.setId(1L); 
     OfyService.ofy().save().entity(truck).now(); 
    } 

    @ApiMethod(name = "gettruck", httpMethod = "get") 
    public Truck getTruck() { 
     return ObjectifyService.ofy().load().key(Key.create(Truck.class, 1L)).now(); 
    } 
} 

看来你已经错过了注册卡车级。这:

static 
{ 
    factory().register(PlaceCategory.class); 
} 

应(如大卫过长...例子所示):

static 
{ 
    factory().register(PlaceCategory.class); 
    factory().register(Truck.class); 
}