jsonrpc4j client使用实例

package com.jiepu.Jsonrpc4j.client;

import java.net.URL;


import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.ProxyUtil;

public class App {
	public static void main(String[] args) throws Throwable {

		test001();
		test002();
	}

	private static void test001() throws Throwable {
		// JsonRpcHttpClient client = new JsonRpcHttpClient(new
		// URL("http://10.0.0.107/json_server/index.php"));
		JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(
				"http://10.0.0.107/json_server/index.php"));
		
		Student student = client.invoke("getstudent", new Object[] {new Student(2, "fuck", 65)}, Student.class);

		System.out.println(student);
		System.out.println(client.invoke("add", new Object[] {14,16}, Integer.class));
		System.out.println(client.invoke("fuck", new Object[] {"java",14,16}, String.class));
	}

	private static void test002() throws Exception {
		JsonRpcHttpClient client = new JsonRpcHttpClient(new URL(
				"http://10.0.0.107/json_server/index.php"));

		IMyService service = ProxyUtil.createClientProxy(new App().getClass()
				.getClassLoader(), IMyService.class, client);

		Student student = service.getstudent(new Student(12, "name", 63));
		System.out.println(student);
		System.out.println(service.add(15, 65));
		System.out.println(service.fuck("xx中wen",45,6));
	}
}
package com.jiepu.Jsonrpc4j.client;

public interface IMyService {
	
	public Integer add(Integer a, Integer b) throws Exception ;
	
	public String fuck(String str,Integer a, Integer b)  throws Exception;
	
	public Student getstudent(Student b) throws Exception;
}

package com.jiepu.Jsonrpc4j.client;

public class Student {

	private Integer id;
	private String name;
	private Integer age;
	
	public Student(){}
	
	public Student(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [age=" + age + ", id=" + id + ", name=" + name + "]";
	}
	
	
}

下载:

https://github.com/briandilley/jsonrpc4j

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.jiepu</groupId>
	<artifactId>Jsonrpc4j</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>Jsonrpc4j</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<!-- jsonrpc4j -->
		<dependency>
			<groupId>com.github.briandilley.jsonrpc4j</groupId>
			<artifactId>jsonrpc4j</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1-b06</version>
		</dependency>
	</dependencies>
</project>



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值