gae json web 服务

在网上找到一篇GAE上建JSON WEB服务的文章

Json REST Java Service for Google App Engine Example

1. Download Eclipse and the Google Plugin for Eclipse ( I use Eclipse IDE for Java EE Developers Helios 3.6)

http://code.google.com/eclipse/docs/download.html

2. Down Restlet  (I use 2.0.5 stable zip file)

http://www.restlet.org/downloads/stable

3. Create a Web Application Project

 

 

 

 

Like this.

 


4. Copy these files to /war/WEB-INF/lib

org.json.jar (in D:\DevTools\restlet-gae-2.0.5\lib\org.json_2.0)
org.restlet.ext.json.jar (in D:\DevTools\restlet-gae-2.0.5\lib)
org.restlet.ext.servlet.jar (in D:\DevTools\restlet-gae-2.0.5\lib)
org.restlet.jar (in D:\DevTools\restlet-gae-2.0.5\lib)

Look like this,

 

 


5. Add JARS… to Libaries

 





Like this,




6. Change MyFirstRestServerServlet.java

package com.example.myfirstrestserver;

 

import org.restlet.Application;

import org.restlet.Restlet;

import org.restlet.routing.Router;

 

public class MyFirstRestServerServlet extends Application{

 

/**

 * Creates a root Restlet that will receive all incoming calls.

 */

@Override

public synchronized Restlet createRoot() {

// Create a router Restlet that routes each call to a new Resource

Router router = new Router(getContext());

 

router.attachDefault(TestJsonResource.class);

return router;

}

}


Like this,

 





7. Make a new java class called TestJsonResource,

 

 

 

package com.example.myfirstrestserver;

 

import java.io.IOException;

import org.json.JSONException;

import org.json.JSONObject;

import org.restlet.ext.json.JsonRepresentation;

import org.restlet.resource.Get;

import org.restlet.resource.ServerResource;

 

public class TestJsonResource extends ServerResource {

 

@Get("json")

public String handleGet() {

try {

JSONObject json = new JSONObject();

json.put("name", "value");

 

JsonRepresentation jsonRep = new JsonRepresentation(json);

 

return jsonRep.getText();

} catch (JSONException e) {

e.printStackTrace();

} catch (IOException e)

{

e.printStackTrace();

}

return null;

}

}


Like this,

 





8. Change web.xml file,

xml version="1.0" encoding="utf-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

 

 

<context-param>

<param-name>org.restlet.applicationparam-name>

<param-value>com.example.myfirstrestserver.MyFirstRestServerServletparam-value>

context-param>

 

 

<servlet>

<servlet-name>MyFirstRestServerservlet-name>

<servlet-class>org.restlet.ext.servlet.ServerServletservlet-class>

servlet>

 

<servlet-mapping>

<servlet-name>MyFirstRestServerservlet-name>

<url-pattern>/*url-pattern>

servlet-mapping>

<welcome-file-list>

<welcome-file>index.htmlwelcome-file>

welcome-file-list>

web-app>



9. Run.

 

原文地址:https://www.cnblogs.com/redmondfan/p/3107408.html