如何制作 RPC-JSON java 服务器

问题描述 投票:0回答:1

我在 Android 中有一个客户端 RPC-JSON,我正在尝试使用 Java 库创建一个 RPC-JSON 服务器(http://software.dzhuvinov.com/json-rpc-2.0-server.html)。这是官方的例子:

// The JSON-RPC 2.0 Base classes that define the 
// JSON-RPC 2.0 protocol messages
import com.thetransactioncompany.jsonrpc2.*;

// The JSON-RPC 2.0 server framework package
import com.thetransactioncompany.jsonrpc2.server.*;

import java.text.*;
import java.util.*;


/**
 * Demonstration of the JSON-RPC 2.0 Server framework usage. The request
 * handlers are implemented as static nested classes for convenience, but in 
 * real life applications may be defined as regular classes within their old 
 * source files.
 *
 * @author Vladimir Dzhuvinov
 * @version 2011-03-05
 */ 
public class Example {


    // Implements a handler for an "echo" JSON-RPC method
    public static class EchoHandler implements RequestHandler {


        // Reports the method names of the handled requests
        public String[] handledRequests() {

            return new String[]{"echo"};
        }


         // Processes the requests
         public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

             if (req.getMethod().equals("echo")) {

                 // Echo first parameter

                 List params = (List)req.getParams();

             Object input = params.get(0);

             return new JSONRPC2Response(input, req.getID());
            }
            else {

                // Method name not supported

                return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
        }
        }
    }


    // Implements a handler for "getDate" and "getTime" JSON-RPC methods
    // that return the current date and time
    public static class DateTimeHandler implements RequestHandler {


        // Reports the method names of the handled requests
    public String[] handledRequests() {

        return new String[]{"getDate", "getTime"};
    }


    // Processes the requests
    public JSONRPC2Response process(JSONRPC2Request req, MessageContext ctx) {

        if (req.getMethod().equals("getDate")) {

            DateFormat df = DateFormat.getDateInstance();

        String date = df.format(new Date());

        return new JSONRPC2Response(date, req.getID());

            }
            else if (req.getMethod().equals("getTime")) {

            DateFormat df = DateFormat.getTimeInstance();

        String time = df.format(new Date());

        return new JSONRPC2Response(time, req.getID());
            }
        else {

            // Method name not supported

        return new JSONRPC2Response(JSONRPC2Error.METHOD_NOT_FOUND, req.getID());
            }
        }
    }


    public static void main(String[] args) {

        // Create a new JSON-RPC 2.0 request dispatcher
    Dispatcher dispatcher =  new Dispatcher();


    // Register the "echo", "getDate" and "getTime" handlers with it
    dispatcher.register(new EchoHandler());
    dispatcher.register(new DateTimeHandler());


    // Simulate an "echo" JSON-RPC 2.0 request
    List echoParam = new LinkedList();
    echoParam.add("Hello world!");

    JSONRPC2Request req = new JSONRPC2Request("echo", echoParam, "req-id-01");
    System.out.println("Request: \n" + req);

    JSONRPC2Response resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);

    // Simulate a "getDate" JSON-RPC 2.0 request
    req = new JSONRPC2Request("getDate", "req-id-02");
    System.out.println("Request: \n" + req);

    resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);

    // Simulate a "getTime" JSON-RPC 2.0 request
    req = new JSONRPC2Request("getTime", "req-id-03");
    System.out.println("Request: \n" + req);

    resp = dispatcher.process(req, null);
    System.out.println("Response: \n" + resp);
    }
}

我在手册和谷歌中搜索,但我不明白如何让服务器等待请求并发送响应。我怎样才能做到?

java json rpc json-rpc
1个回答
0
投票

此示例演示了请求分派。示例中根本没有涵盖整个 HttpServer 内容。 Google,如何使用 Java 设置 HttpServer。然后,您必须在 HttpExchange 上调用 getRequestBody(),解析请求正文字符串以生成请求对象。这就是您的样本开始的地方。

© www.soinside.com 2019 - 2024. All rights reserved.