我有一个 serverlet,它可以接受来自休息服务或来自 jsp 表单帖子的请求,它们都将调用内部方法 (internalAddPodcast()) 以将实体添加到数据存储区。
当我从 jsp 页面点击 internalAddPodcast() 时,它工作正常,我可以通过在添加后立即查询实体来看到该实体已成功添加。但是,当我从其他方法 addPodcast() 执行此操作时, datastore.put() 似乎并没有真正添加到数据存储中,因为我尝试在 put() 之后立即检索它并且什么都没有返回。往下看这个类的底部,我在其中添加了注释“//当从 REST 服务添加时,此查询为空:(”这是我希望得到一些结果的地方,尤其是我刚刚放入的实体数据存储。
package com.aol.sharepodder;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
@Path("/add/podcast/")
public class AddPodcastServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(AddPodcastServlet.class
.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String email = req.getParameter("email");
String collectionName = req.getParameter("collectionName");
String podcast_url = req.getParameter("podcast_url");
String podcast_description = req.getParameter("podcast_description");
String podcast_title = req.getParameter("podcast_title");
log.info("--post adding " + collectionName);
internalAddPodcast(email, collectionName, podcast_url,
podcast_description, podcast_title);
resp.sendRedirect("/collection_details.jsp?collectionName="
+ collectionName + "&email=" + email);
}
@POST
@Produces("text/plain")
@Consumes("application/x-www-form-urlencoded")
public String addPodcast(
@DefaultValue("barrand@gmail.com") @FormParam("email") String email,
@DefaultValue("default") @FormParam("collectionName") String collectionName,
@DefaultValue("") @FormParam("podcast_url") String podcast_url,
@DefaultValue("") @FormParam("podcast_description") String podcast_description,
@DefaultValue("") @FormParam("podcast_title") String podcast_title) {
try {
internalAddPodcast(email, collectionName, podcast_url,
podcast_description, podcast_title);
if (podcast_url == "") {
return "No url supplied";
}
return "true";
} catch (Exception e) {
return e.getMessage();
}
}
private void internalAddPodcast(String email, String collectionName,
String podcast_url, String podcast_description, String podcast_title) {
log.info("--INTERNAL ADD ");
log.info("--email " + email);
log.info("--collectionName " + collectionName);
log.info("--podcast_url " + podcast_url);
log.info("--podcast_description " + podcast_description);
log.info("--podcast_title " + podcast_title);
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
Entity podcast = new Entity("Podcast");
podcast.setProperty("collectionName", collectionName);
podcast.setProperty("user", user);
podcast.setProperty("email", email);
Date date = new Date();
podcast.setProperty("date", date);
podcast.setProperty("podcast_title", podcast_title);
podcast.setProperty("podcast_description", podcast_description);
podcast.setProperty("podcast_url", podcast_url);
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
datastore.put(podcast);
//try to log the podcast that I just got done adding to the datastore
Query query = new Query("Podcast");
PreparedQuery pq = datastore.prepare(query);
//THIS QUERY IS EMPTY WHEN ADDED FROM THE REST SERVICE :(
for (Entity p : pq.asIterable()) {
log.info("_loop " + " - " + KeyFactory.keyToString(p.getKey())
+ " -- " + p.getProperty("podcast_title") + " - "
+ p.getProperty("podcast_url"));
}
}
}
任何想法我做错了什么,以及为什么我试图从 rest 方法添加的实体没有被添加到数据存储中。
我知道,在这两种情况下,(来自 jsp 帖子或其余服务)当我到达 internalAddPodcast() 时,所有方法参数都正确输入。