开发者

Simple GAE Java JSON REST Server

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开发者_StackOverflow World" string and I'm done?


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.


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/


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.


Try with Jersey!

http://jersey.java.net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜