• 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.

     

  • 相关阅读:
    NetCore3.1,NetCore5.0让发布的时候可以展示views
    Mac配置Gradle环境变量
    [转] 聊聊OkHttp实现WebSocket细节,包括鉴权和长连接保活及其原理!
    spark hive 数据不一致 spark默认本地数据元 spark不能插入hive数据
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb8 in position 885: invalid start byte
    hadoop+zookeeper+yarn+spark高可用主从备份启动步骤
    Java笔记
    cnblogs设置
    IC Test Note
    复制文件夹下所有文件文件名
  • 原文地址:https://www.cnblogs.com/redmondfan/p/3107408.html
Copyright © 2020-2023  润新知