我刚刚开始学习GAE,现在我正在使用GAE和eclipse开发一个简单的服务。我使用Objectify创建了3个实体,但是当我访问http://localhost:8080/_ah/admin/
时,Entity Kind组合框是空的。
这是我的客观化代码:
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();
}
}
这是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 + "]";
}
}
这是我的终极代码:
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;
}
}
最后,这是我的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());
}
而且,我在javascript中的sayHelloCallback函数仅在警报中显示“false”。
我哪里出错了? 请帮我在数据存储区中创建实体。 谢谢:))
答案 0 :(得分:2)
您的问题可能是由于在客观化实体中没有非arg构造函数。
必须有一个no-arg构造函数(或者没有构造函数--Java创建一个默认的无参数构造函数)。
https://github.com/objectify/objectify/wiki/Entities#the-basics
这应该有所帮助。
private Clients(){}