简单的 GAE Java JSON REST 服务器
我是 GAE、JSON、REST 和一般 Web 领域的新手。我有一个非常简单的 GWT/GAE 数据存储,我想用它来更新我编写的 Android 应用程序中的数据。经过一些研究,似乎一个好方法是创建一个 RESTful Web 服务,我的 Android 应用程序将通过发送回 JSON 格式数据的 HTTP 请求进行访问。
我一直在寻找一种方法来做到这一点,似乎有很多方法,主要是使用第三方库。种类繁多,作为初学者很难做出明智的选择。另外,我的问题非常简单,这将是学习在 GAE 上创建 RESTful Web 服务和 JSON 的基础知识的好机会。它所需要做的就是从 URL 返回 JSON 数据 - 不创建、不更新、不删除。
我想一个简单的 Java 代码示例或教程比学习一个复杂的库更能实现我的目标。有谁知道一个简单的例子或教程可以引导我走向正确的方向?
谢谢。
请不要恶搞
== 更新:
在来自向导的 GreetingServiceImpl 的 doGet @Override 中,我添加了:
// super.doGet(req, resp);
PrintWriter out = resp.getWriter();
out.println("Hello World");
这似乎有效。事情能有这么简单吗?我可以修改此代码来生成 JSON 输出来代替“Hello World”字符串吗?
I'm a Newbie at GAE, JSON, REST and the web in general. I have a very simple GWT/GAE datastore I would like to use to update data in an Android application I wrote. After some research, it seems a good way to do this is to create a RESTful web service my Android App will access through an HTTP request that sends back JSON formatted data.
I've been looking for a way to do this and there seems to be many ways, mostly using 3rd party libraries. There are so many, it's hard to choose wisely as a beginner. Also, my problem is so simple that this would be a good chance to learn the basics of creating a RESTful web service and JSON on GAE. All it needs to do is return JSON data from a URL - no creates, no updates, no deletes.
I imagine a simple example or tutorial of Java code would make more sense to achieve my goal than learning a complicated library. Does anyone know of a simple example or tutorial to send me in the right direction?
Thanks.
No Snark Please
==
Update:
In the doGet @Override of GreetingServiceImpl that comes from the wizard, I added:
// super.doGet(req, resp);
PrintWriter out = resp.getWriter();
out.println("Hello World");
This seems to work. Could it be this simple? Can I just modify this code to generate my JSON output in place of the "Hello World" string and I'm done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的问题有点笼统(无意讽刺),所以我的回答也很笼统。尽管我的回答很笼统,但这些信息应该可以为您指明正确的方向。如果您的问题变得更加具体,请编辑您原来的问题,或者询问其他问题是否足够具体。
我想说最简单的方法是获取 Eclipse,并使用 GAE Eclipse 插件(如果您还没有)。
请按照此处的说明安装、创建和运行项目:
http:// /code.google.com/appengine/docs/java/tools/eclipse.html
完成所有这些操作后,在 servlet 中重写 doGet 方法以使用访问数据存储区所需的任何代码。一旦您获得了想要从数据存储中获取对象的代码,我将使用 GSON 将生成的对象序列化为 JSON。 http://code.google.com/p/google-gson/
再说一次,这并不是您需要的全部信息。请随意编辑您的问题或评论并询问更多具体信息。另外,您提到您也一直在寻找其他第三方库来执行此操作。这里要小心。您必须记住,如果您使用的是谷歌应用程序引擎,则在本地工作的东西在部署时可能无法工作。这有点不便,但它是谷歌简化应用程序引擎并提供尽可能多的免费托管的一部分。有一个第三方库列表,这些库已经在 Java 的应用程序引擎环境中经过了实际测试;但我无法通过粗略的谷歌搜索找到它。我知道它在应用程序引擎文档中的某个地方。而且,就其价值而言,我在应用程序引擎中对 GSON 进行了一次小型冒烟测试,它似乎工作得很好。
You're question is kind of general (no snark intended) so my answer is general too. Even though my answer is general, the information should get you pointed in the right direction. If your question becomes more specific, either edit your original question or ask another if it is specific enough.
I would say the easiest way to do this would be to get Eclipse, and use the GAE Eclipse plugin (if you haven't already).
Follow the instructions here to install, create, and run a project:
http://code.google.com/appengine/docs/java/tools/eclipse.html
Once you've done all of this, in your servlet, rewrite the doGet method to use any code you have to hit your datastore. Once you have the code you want to get your object from your datastore, I would serialize the resulting Object(s) to JSON using GSON. http://code.google.com/p/google-gson/
Again, this isn't all of the information you're going to need. Feel free to edit your question or comment and ask for more specific information. Also, you mentioned you've been looking at other 3rd party libraries to do this as well. Be careful here. You have to keep in mind that if you're using google app engine, something that works locally might not work when you deploy it. This is kind of an inconvenience but its part of what allows Google to simplify app engine and offer as much hosting for free as it does. There is a list of 3rd party libraries which have been battle tested in the app engine environment for Java; but I wasn't able to find it with a cursory google search. I know its somewhere in the app engine docs though. And, for what its worth, I did a small smoke test of GSON in app engine and it seemed to work fine.
尝试使用 JSONEngine - 该库创建易于访问的 JSON API,用于在 Google App Engine 中存储/检索/更新/删除 JSON 数据。对每个 JSON 文档的访问权限可以设置为特定用户/仅限 google 电子邮件 ID/仅限 open id 电子邮件 ID(例如 Google/Yahoo/AOL)...
有关此库的更多信息,请访问 code.google.com/p/jsonengine/
Try using JSONEngine- this library creates easily accessible JSON APIs for storing/retrieving/updating/deleting JSON data in Google App Engine. Access to each JSON document can be set to specific user/ google email ids only/open id email IDs only(like Google/Yahoo/AOL)...
Read more about this library at code.google.com/p/jsonengine/
尝试新的 Google Cloud Endpoints。它们仍处于实验阶段,但据我尝试,它们工作得很好。
此外,它们非常易于用于您定义的简单目的。如果您使用 Google Eclipse 插件,则只需创建一个 JDO 持久类,插件就会自动为您生成端点。请此处查看 Google Eclipse 插件对 Google Cloud Endpoints 的支持。
Try new Google Cloud Endpoints. They are still experimental, but as far as I have tried, they work perfectly.
Moreover they are extremely easy to use for simple purposes as you define. If you are using Google Eclipse Plugin you only have to create a JDO persitent class and the plugin generates the endpoint for you automatically. See Google Eclipse Plugin support for Google CLoud Endpoints here.
尝试使用泽西岛!
http://jersey.java.net/
Try with Jersey!
http://jersey.java.net/