gpt4 book ai didi

java - 无法在 Eclipse 中创建 GAE 实体

转载 作者:行者123 更新时间:2023-12-02 04:33:44 26 4
gpt4 key购买 nike

我刚刚开始学习 GAE,现在我正在使用 GAE 和 eclipse 开发一个简单的服务。我使用 Objectify 创建了 3 个实体,但是当我访问 http://localhost:8080/_ah/admin/ 时,实体种类组合框为空。

这是我的对象化代码:

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”。

我哪里出错了?请帮助我在数据存储中创建实体。谢谢:)

最佳答案

您的问题可能是由于 objectify 实体中没有无参数构造函数引起的。

There must be a no-arg constructor (or no constructors - Java creates a default no-arg constructor).

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

这应该有帮助。

private Clients(){}

关于java - 无法在 Eclipse 中创建 GAE 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31100632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com