简体   繁体   English

无法在Eclipse中创建GAE实体

[英]Can't Create GAE Entity in Eclipse

I just started learning GAE and now I'm developing a simple service using GAE and eclipse. 我刚刚开始学习GAE,现在我正在使用GAE和Eclipse开发一个简单的服务。 I created 3 entities using Objectify, but when I access http://localhost:8080/_ah/admin/ the Entity Kind combobox is empty. 我使用Objectify创建了3个实体,但是当我访问http://localhost:8080/_ah/admin/ ,“实体种类”组合框为空。

Here's my Objectify Code : 这是我的对象化代码:

package com.google.training.helloworld;

//import all entity
import com.google.training.helloworld.Clients;
import com.google.training.helloworld.Posts;
import com.google.training.helloworld.Users;

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

/**
 * Custom Objectify Service that this application should use.
 */
public class OfyService {
    /**
     * This static block ensure the entity registration.
     */
    static {
        // register all entity
        factory().register(Clients.class);
        factory().register(Posts.class);
        factory().register(Users.class);
    }

    /**
     * Use this static method for getting the Objectify service object in order to make sure the
     * above static block is executed before using Objectify.
     * @return Objectify service object.
     */
    public static Objectify ofy() {
        return ObjectifyService.ofy();
    }

    /**
     * Use this static method for getting the Objectify service factory.
     * @return ObjectifyFactory.
     */
    public static ObjectifyFactory factory() {
        return ObjectifyService.factory();
    }

}

Here's the Clients class code : 这是Clients类代码:

package com.google.training.helloworld;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class Clients {

    @Id private String clientApiKey;
    private String clientName, clientEmail, clientPassword;

    public Clients(String clientApiKey, String clientName, String clientEmail,
            String clientPassword) {
        super();
        this.clientApiKey = clientApiKey;
        this.clientName = clientName;
        this.clientEmail = clientEmail;
        this.clientPassword = clientPassword;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    public String getClientEmail() {
        return clientEmail;
    }

    public void setClientEmail(String clientEmail) {
        this.clientEmail = clientEmail;
    }

    public String getClientPassword() {
        return clientPassword;
    }

    public void setClientPassword(String clientPassword) {
        this.clientPassword = clientPassword;
    }

    public String getClientApiKey() {
        return clientApiKey;
    }

    @Override
    public String toString() {
        return "Clients [clientApiKey=" + clientApiKey + "]";
    }



}

Here's my EndPoint Code : 这是我的EndPoint代码:

package com.google.training.helloworld;

import java.lang.String;
import java.util.Random;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.api.server.spi.config.Named;
import com.google.training.helloworld.OfyService;
import com.googlecode.objectify.cmd.Query;

/**
 * Defines endpoint functions APIs.
 */
@Api(name = "helloworldendpoints", version = "v1",
scopes = {Constants.EMAIL_SCOPE },
        clientIds = {Constants.WEB_CLIENT_ID, Constants.API_EXPLORER_CLIENT_ID },
        description = "API for hello world endpoints.")

public class HelloWorldEndpoints {

    Random rand = new Random();
    OfyService o = new OfyService();

   // Declare this method as a method available externally through Endpoints
    //@ApiMethod(name = "clientLogin", path = "clientLogin",
    //        httpMethod = HttpMethod.POST)

    //public String clientLogin(@Named("apikey") String apikey, @Named("password") String pass) {
    //    return "a";
    //}

    // Declare this method as a method available externally through Endpoints
     @ApiMethod(name = "clientRegister", path = "clientRegister",
             httpMethod = HttpMethod.POST)

     public Clients clientRegister(@Named("name") String name, @Named("email") String email, @Named("password") String pass) {
         OfyService.ofy().factory().begin();

         String newApiKey = "";
         boolean apiKeyRegistered = false;

         do{
             newApiKey = generateApiKey();

             //check whether the apikey is registered
             Query<Clients> q =  OfyService.ofy().load().type(Clients.class).filter("clientApiKey", newApiKey);
             for (Clients car: q) {
                 apiKeyRegistered = true;
             }
         }while(apiKeyRegistered = true);

         // register
         Clients client = new Clients(newApiKey, name, email, pass);
         OfyService.ofy().save().entity(client).now();

         return client;
     }

     private String generateApiKey(){
         String apiKey = "";
         String chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321";
         int charsLength = chars.length();

         for (int i = 0; i < 25; i++){
             int beginIndex = rand.nextInt(24);
             apiKey += chars.substring(beginIndex, beginIndex+1);
         }

         return apiKey;
     }

}

And lastly, here's my javascript code : 最后,这是我的JavaScript代码:

/*
 * http://stackoverflow.com/questions/18260815/use-gapi-client-javascript-to-execute-my-custom-google-api
 * https://developers.google.com/appengine/docs/java/endpoints/consume_js
 * https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientload
 *
 */

/**
 * After the client library has loaded, this init() function is called.
 * The init() function loads the helloworldendpoints API.
 */

function init() {

    // You need to pass the root path when you load your API
    // otherwise calls to execute the API run into a problem

    // rootpath will evaulate to either of these, depending on where the app is running:
    // //localhost:8080/_ah/api
    // //your-app-id/_ah/api

    var rootpath = "//" + window.location.host + "/_ah/api";

    // Load the helloworldendpoints API
    // If loading completes successfully, call loadCallback function
    gapi.client.load('helloworldendpoints', 'v1', loadCallback, rootpath);
}

/*
 * When helloworldendpoints API has loaded, this callback is called.
 * 
 * We need to wait until the helloworldendpoints API has loaded to
 * enable the actions for the buttons in index.html,
 * because the buttons call functions in the helloworldendpoints API
 */
function loadCallback () {  
    // Enable the button actions
    enableButtons ();
}

function enableButtons () {
    // Set the onclick action for the first button
    btn = document.getElementById("btnLogin");
    btn.onclick= function(){login();};

    // Update the button label now that the button is active
    btn.value="Client Login";

    // Set the onclick action for the second button
    btn = document.getElementById("btnRegister");
    btn.onclick=function(){register();};

    // Update the button label now that the button is active
    btn.value="Client Register";

}


/*
 * Execute a request to the login() endpoints function
 */
function login () {
    // Construct the request for the clientLogin() function
    //var request = gapi.client.helloworldendpoints.clientLogin();

    // Execute the request.
    // On success, pass the response to sayHelloCallback()
    //request.execute(sayHelloCallback);
}

/*
 * Execute a request to the register() endpoints function
 */
function register () {
    // Construct the request for the clientRegister() function
    var name = document.getElementById("txtRegisterName").value;
    var email = document.getElementById("txtRegisterEmail").value;
    var pass = document.getElementById("txtRegisterPassword").value;

    alert("requesting");

    var request = gapi.client.helloworldendpoints.clientRegister({"name" : name, "email" : email, "password" : pass});

    alert("requested");
    // Execute the request.
    // On success, pass the response to sayHelloCallback()
    request.execute(sayHelloCallback);
}



// Process the JSON response
// In this case, just show an alert dialog box
// displaying the value of the message field in the response
function sayHelloCallback (response) {
    alert(response.toString()); 
}

And also, my sayHelloCallback function in javascript only shows "false" in alert. 而且,我在javascript中的sayHelloCallback函数在警报中仅显示“ false”。

Where did I go wrong? 我哪里做错了? Please help me with creating the entities in datastore. 请帮助我在数据存储区中创建实体。 Thank you :) 谢谢 :)

Your problem is probably caused by not having no-arg constructor in objectify entity. 您的问题可能是由于objectify实体中没有no-arg构造函数引起的。

There must be a no-arg constructor (or no constructors - Java creates a default no-arg constructor). 必须有一个无参数构造函数(或没有构造函数-Java创建一个默认的无参数构造函数)。

https://github.com/objectify/objectify/wiki/Entities#the-basics https://github.com/objectify/objectify/wiki/Entities#the-basics

This should help. 这应该有所帮助。

private Clients(){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM