2014-09-27 50 views
2

我使用物化和谷歌雲端點來開發應用程序的後端在一次調用保存引用的對象(通過參考文獻)。我有一個「卡車」類,有一個參考(使用參考)的「類別」類。它看起來像這樣:使用雲端點

@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

回答

1

下面的代碼,非常接近你的,很適合我既開發服務器上,並在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(); 
    } 
} 
1

看來你已經錯過了註冊卡車級。這:

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

應(如大衛過長...例子所示):

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