Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 957
Mitch Avatar asked Sep 06 '11 07:09

Mitch


2 Answers

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.

like image 147
Dave Avatar answered Oct 05 '22 07:10

Dave


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/

like image 31
Arvind Avatar answered Oct 05 '22 08:10

Arvind