[英]GAE with JPA and REST: Produce Entity (as json or xml) with the datastore.Key
我有一個這樣的實體:
@Entity
@XmlRootElement
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private com.google.appengine.api.datastore.Key key;
private String content;
private String title;
private Date created;
private Date lastUpdate;
private boolean isActive;
/* public getter and setters */
}
現在,我有一個休息的webserivce,它返回以下文章:
@SuppressWarnings({ "unchecked", "unused" })
@GET
@Path("/{user}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<Article> getArtilceListByUser(@PathParam("user") String userName) {
List<Article> articles = null;
EntityManager em = EMF.get().createEntityManager();
try {
articles = (List<Article>) em.createQuery("SELECT FROM Article WHERE createdBy = :user AND isActive = true")
.setParameter("user", userName)
.getResultList();
// lazy load is activated, but em is closed (find a better solution)
for (Article article : articles) ;
} finally {
em.close();
}
return articles;
}
問題是, com.google.appengine.api.datastore.Key的對象無法序列化為json或xml,但是我需要文章的ID ...所以我必須使用長類型作為ID或添加一個額外的字段來包裝密鑰? 您對此問題有更好的解決方案嗎?
com.google.appengine.api.datastore.KeyFactory.keyToString
接着
com.google.appengine.api.datastore.KeyFactory.stringToKey
這些僅出於此目的創建和使用“ websafe”密鑰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.