java和php实现JSON-RPC 实例

php server:

<?php

//http://php.net/manual/en/class.soapserver.php

include 'entitys.php';
require_once 'Zend/Json/Server.php';

$server = new Zend_Json_Server();

$server->setClass('Calculator');

if ('GET' == $_SERVER['REQUEST_METHOD']) {
    $server->setTarget('/json-rpc.php')->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
    $smd = $server->getServiceMap();

    // Set Dojo compatibility:
    $smd->setDojoCompatible(true);

    header('Content-Type: application/json');
    echo $smd;
    return;
}

$server->handle();

?>
<pre name="code" class="php"><?php

class Student{
    public $id;
    public $name;
    public $age;
}
/**
 * Calculator - sample class to expose via JSON-RPC
 */
class Calculator
{
    /**
     * Return sum of two variables
     *
     * @param  int $x
     * @param  int $y
     * @return int
     */
    public function add($x, $y)
    {
        return $x + $y;
    }

    /**
     * Return difference of two variables
     *
     * @param  int $x
     * @param  int $y
     * @return int
     */
    public function subtract($x, $y)
    {
        return $x - $y;
    }

    /**
     * Return product of two variables
     *
     * @param  int $x
     * @param  int $y
     * @return int
     */
    public function multiply($x, $y)
    {
        return $x * $y;
    }

    /**
     * Return the division of two variables
     *
     * @param  int $x
     * @param  int $y
     * @return float
     */
    public function divide($x, $y)
    {
        return $x / $y;
    }
    public function fuck($x, $y)
    {
        $m=$x +$y;
        return 'fuck 日本'.$m;
    }

    public function getstudent($student)
    {
        $student=new Student();
        $student->id=110;
        $student->name='php';
        $student->age=21;
        return $student;
    }
}
?>

 
 
php client 

<?php
include ("lib/client/JsonRpcClient.php");
include ("lib/client/AuthenticatedJsonRpcClient.php");

//https://github.com/Pozo/json-rpc-php
$client = new JsonRpcClient('http://localhost/json_server/server.php');

echo '<pre>';
var_dump($client->add(22, 9));
echo '</pre>';

echo '<pre>';
var_dump($client->fuck(21,32));
echo '</pre>';

?>

java server:

package com.jiepu.server;

import cz.eman.jsonrpc.server.tcp.TcpJsonMultiServer;

public class WebService implements IMyService {
	public Integer add(Integer a, Integer b) {
		return a + b;
	}

	public String hello(String data) {

		return "hello,你好" + data;
	}

	public Student echo(Student b) throws Exception {

		b.setName("newname");
		b.setAge(12);
		return b;

	}

	public static void main(String[] args) throws Exception {

		// new TcpJsonSingleServer(new WebService(), 20000).start();
		new TcpJsonMultiServer(new WebService(), 20000).start();
	}

}
package com.jiepu.server;

public interface IMyService {
	public Integer add(Integer a, Integer b) throws Exception ;
	
	public String hello(String data)  throws Exception;
	
	public Student echo(Student b) throws Exception;
}

package com.jiepu.server;

public interface IMyService {
	public Integer add(Integer a, Integer b) throws Exception ;
	
	public String hello(String data)  throws Exception;
	
	public Student echo(Student b) throws Exception;
}

java client

package com.jiepu.client;


import java.net.InetSocketAddress;

import com.jiepu.server.IMyService;
import com.jiepu.server.Student;

import cz.eman.jsonrpc.client.AbstractClientProxy;
import cz.eman.jsonrpc.client.ClientProvider;
import cz.eman.jsonrpc.client.TcpJsonClient;

public class WebServiceClientProxy extends AbstractClientProxy<IMyService> implements IMyService {

	public WebServiceClientProxy(ClientProvider clientProvider) {
		super(IMyService.class, clientProvider);
	}

	public Integer add(Integer a, Integer b) throws Exception {
		return (Integer) super.callMethod("add", new Object[] { a, b });
	}

	public String hello(String data)  throws Exception{
		return (String) super.callMethod("hello", new Object[] { data});
	}

	public Student echo(Student b) throws Exception {
		
		return (Student) super.callMethod("echo", b);		
	}
	
	public static void main(String[] args) throws Exception {
		
		WebServiceClientProxy proxy = new WebServiceClientProxy(new TcpJsonClient(new InetSocketAddress("localhost", 20000)));
		System.out.println(proxy.add(19, 12));
		System.out.println(proxy.hello("fuck"));
		System.out.println(proxy.echo(new Student()));
		
		
	}

}

package com.jiepu.client;


import java.net.URL;

import com.jiepu.server.Student;

import cz.eman.jsonrpc.client.AbstractClientProxy;
import cz.eman.jsonrpc.client.HttpJsonClient;

//http://java-json-rpc.sourceforge.net/
public class TestHttp {

	public static void main(String[] args) throws Exception {
		
		HttpJsonClient client=new HttpJsonClient(new URL("http://10.0.0.107/json_server/index.php"));
		AbstractClientProxy proxy;
		proxy = new AbstractClientProxy(null, client, false);
		System.out.println(proxy.callNativeMethod("add", 12, 14));
		System.out.println(proxy.callNativeMethod("fuck", 32, 4));
		System.out.println(proxy.callNativeMethod("multiply", 14,12));
		String data=proxy.callNativeMethod("getstudent", new Student(1,"name",123));
		System.out.println(data);
	}
}
package com.jiepu.client;

import java.net.InetSocketAddress;
import java.net.Socket;

import cz.eman.jsonrpc.client.AbstractClientProxy;
import cz.eman.jsonrpc.client.TcpJsonClient;

//http://java-json-rpc.sourceforge.net/
//http://www.json-rpc.org/wiki/implementations
public class Test {

	public static void main(String[] args) throws Exception {

		// HttpJsonClient client=new HttpJsonClient(new URL("http://localhost/json-rpc-testserver/jsonrpc/test1"));
		AbstractClientProxy proxy;
		TcpJsonClient client = new TcpJsonClient(new InetSocketAddress(
				"localhost", 20000));

		System.out.println(client.toString());
		proxy = new AbstractClientProxy(null, client, false);
		proxy.callNativeMethod("add", 12, 14);
		proxy.callNativeMethod("hello", "呵呵zz");

	}
}

源码工程打包在百度云:

http://pan.baidu.com/s/1bnde5bT




你可能感兴趣的:(java和php实现JSON-RPC 实例)