JSON-RPC for Java

https://github.com/briandilley/jsonrpc4j#json-rpc-for-java

This project aims to provide the facility to easily implement JSON-RPC for the java programming language. jsonrpc4j uses the Jackson library to convert java objects to and from json objects (and other things related to JSON-RPC).

Features Include:

  • Streaming server (InputStream \ OutputStream)
  • HTTP Server (HttpServletRequest \ HttpServletResponse)
  • Portlet Server (ResourceRequest \ ResourceResponse)
  • Socket Server (StreamServer)
  • Integration with the Spring Framework (RemoteExporter)
  • Streaming client
  • HTTP client
  • Dynamic client proxies
  • Annotations support
  • Custom error resolving
  • Composite services

Maven

This project is built with Maven. Be sure to check the pom.xml for the dependencies if you're not using maven. If you're already using spring you should have most (if not all) of the dependencies already - outside of maybe the Jackson Library. Jsonrpc4j is available from the maven central repo. Add the following to your pom.xml if you're using maven:

In <dependencies>:

    <!-- jsonrpc4j -->
<dependency>
<groupId>com.github.briandilley.jsonrpc4j</groupId>
<artifactId>jsonrpc4j</artifactId>
<version>1.1</version>
</dependency>

JSON-RPC specification

The official source for the JSON-RPC 2.0 specification. The guys over at json-rpc google group seem to be fairly active, so you can ask clarifying questions there.

Streaming server and client

Jsonrpc4j comes with a streaming server and client to support applications of all types (not just HTTP). The JsonRpcClient and JsonRpcServer have simple methods that take InputStreams andOutputStreams. Also in the library is a JsonRpcHttpClient which extends the JsonRpcClient to add HTTP support.

Spring Framework

jsonrpc4j provides a RemoteExporter to expose java services as JSON-RPC over HTTP without requiring any additional work on the part of the programmer. The following example explains how to use the JsonServiceExporter within the Spring Framework.

Create your service interface:

package com.mycompany;
public interface UserService {
User createUser(String userName, String firstName, String password);
User createUser(String userName, String password);
User findUserByUserName(String userName);
int getUserCount();
}

Implement it:

package com.mycompany;
public class UserServiceImpl
implements UserService { public User createUser(String userName, String firstName, String password) {
User user = new User();
user.setUserName(userName);
user.setFirstName(firstName);
user.setPassword(password);
database.saveUser(user)
return user;
} public User createUser(String userName, String password) {
return this.createUser(userName, null, password);
} public User findUserByUserName(String userName) {
return database.findUserByUserName(userName);
} public int getUserCount() {
return database.getUserCount();
} }

Configure your service in spring as you would any other RemoteExporter:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="userService" class="com.mycompany.UserServiceImpl">
</bean> <bean name="/UserService.json" class="com.googlecode.jsonrpc4j.spring.JsonServiceExporter">
<property name="service" ref="userService"/>
<property name="serviceInterface" value="com.mycompany.UserService"/>
</bean> </beans>

Your service is now available at the URL /UserService.json. Type conversion of JSON->Java and Java->JSON will happen for you automatically. This service can be accessed by any JSON-RPC capable client, including the JsonProxyFactoryBeanJsonRpcClient and JsonRpcHttpClient provided by this project:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.googlecode.jsonrpc4j.spring.JsonProxyFactoryBean">
<property name="serviceUrl" value="http://example.com/UserService.json"/>
<property name="serviceInterface" value="com.mycompany.UserService"/>
</bean> <beans>

In the case that your JSON-RPC requires named based parameters rather than indexed parameters an annotation can be added to your service interface (this also works on the service implementation for the ServiceExporter):

package com.mycompany;
public interface UserService {
User createUser(@JsonRpcParamName("theUserName") String userName, @JsonRpcParamName("thePassword") String password);
}

By default all error message responses contain the the message as returned by Exception.getmessage() with a code of 0. This is not always desirable. jsonrpc4j supports annotated based customization of these error messages and codes, for example:

package com.mycompany;
public interface UserService {
@JsonRpcErrors({
@JsonRpcError(exception=UserExistsException.class,
code=-5678, message="User already exists", data="The Data"),
@JsonRpcError(exception=Throwable.class,code=-187)
})
User createUser(@JsonRpcParamName("theUserName") String userName, @JsonRpcParamName("thePassword") String password);
}

The previous example will return the error code -5678 with the message User already exists if the service throws a UserExistsException. In the case of any other exception the code -187 is returned with the value of getMessage() as returned by the exception itself.

Auto Discovery With Annotations

Spring can also be configured to auto-discover services and clients with annotations.

To configure auto-discovery of annotated services first annotate the service interface:

@JsonRpcService("/path/to/MyService")
interface MyService {
... service methods ...
}

and use the following configuration to allow spring to find it:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean class="com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceExporter"/> <bean class="com.mycompany.MyServiceImpl" /> </beans>

Configuring a client is just as easy:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean class="com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator">
<property name="baseUrl" value="http://hostname/api/" />
<property name="scanPackage" value="com.mycompany.services" />
</bean> </beans>

Where the baseUrl is added to the front of the path value provided by the JsonRpcService annotation and scanPackage tells spring which packages to scan for services.

Without the Spring Framework

jsonrpc4j can be used without the spring framework as well. In fact, the client and server both work in an Android environment.

Client

Here's an example of how to use the client to communicate with the JSON-RPC service described above:

JsonRpcHttpClient client = new JsonRpcHttpClient(
new URL("http://example.com/UserService.json")); User user = client.invoke("createUser", new Object[] { "bob", "the builder" }, User.class);

Or, the ProxyUtil class can be used in conjunction with the interface to create a dynamic proxy:

JsonRpcHttpClient client = new JsonRpcHttpClient(
new URL("http://example.com/UserService.json")); UserService userService = ProxyUtil.createClientProxy(
getClass().getClassLoader(),
UserService.class,
client); User user = userService.createUser("bob", "the builder");

server

The server can be used without spring as well:

// create it
JsonRpcServer server = new JsonRpcServer(userService, UserService.class);

After having created the server it's simply a matter of calling one of the handle(...) methods available. For example, here's a servlet using the very same UserService:

class UserServiceServlet
extends HttpServlet { private UserService userService;
private JsonRpcServer jsonRpcServer; protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
jsonRpcServer.handle(req, resp);
} public void init(ServletConfig config) {
this.userService = ...;
this.jsonRpcServer = new JsonRpcServer(this.userService, UserService.class);
} }

Composite Services

Multiple services can be combined into a single server using one of theProxyUtil::createCompositeService(...) methods. For example:

UserverService userService = ...;
ContentService contentService = ...;
BlackJackService blackJackService = ...; Object compositeService = ProxyUtil.createCompositeServiceProxy(
this.getClass().getClassLoader(),
new Object[] { userService, contentService, blackJackService},
new Class<?>[] { UserService.class, ContentService.class, BlackJackService.class},
true); // now compositeService can be used as any of the above service, ie:
User user = ((UserverService)compositService).createUser(...);
Content content = ((ContentService)compositService).getContent(...);
Hand hand = ((BlackJackService)compositService).dealHand(...);

This can be used in conjunction with the JsonRpcServer to expose the service methods from all services at a single location:

JsonRpcServer jsonRpcServer = new JsonRpcServer(compositeService);

A spring service exporter exists for creating composite services as well namedCompositeJsonServiceExporter.

Streaming (Socket) Server

A streaming server that uses Sockets is available in the form of the StreamServer class. It's use is very straitforward:

// create the jsonRpcServer
JsonRpcServer jsonRpcServer = new JsonRpcServer(...); // create the stream server
int maxThreads = 50;
int port = 1420;
InetAddress bindAddress = InetAddress.getByName("...");
StreamServer streamServer = new StreamServer(
jsonRpcServer, maxThreads, port, bindAddress); // start it, this method doesn't block
streamServer.start();

and when you're ready to shut the server down:

// stop it, this method blocks until
// shutdown is complete
streamServer.stop();

Of course, this is all possible in the Spring Framework as well:

    <bean id="streamingCompositeService" class="com.googlecode.jsonrpc4j.spring.CompositeJsonStreamServiceExporter">
<!-- can be an IP, hostname or omitted to listen on all available devices -->
<property name="hostName" value="localhost"/>
<property name="port" value="6420"/>
<property name="services">
<list>
<ref bean="userService" />
<ref bean="contentServic" />
<ref bean="blackJackService" />
</list>
</property>
</bean>

JsonRpcServer settings explained

The following settings apply to both the JsonRpcServer and JsonServiceExporter:

  • allowLessParams - Boolean specifying whether or not the server should allow for methods to be invoked by clients supplying less than the required number of parameters to the method.
  • allowExtraParams - Boolean specifying whether or not the server should allow for methods to be invoked by clients supplying more than the required number of parameters to the method.
  • rethrowExceptions - Boolean specifying whether or not the server should re-throw exceptions after sending them back to the client.
  • backwardsComaptible - Boolean specifying whether or not the server should allow for jsonrpc 1.0 calls. This only includes the omission of the jsonrpc property of the request object, it will not enable class hinting.
  • errorResolver - An implementation of the ErrorResolver interface that resolves exception thrown by services into meaningful responses to be sent to clients. Multiple ErrorResolvers can be configured using the MultipleErrorResolver implementation of this interface.

Server Method resolution

Methods are resolved in the following way, each step immediately short circuits the process when the available methods is 1 or less.

  1. All methods with the same name as the request method are considered
  2. If allowLessParams is disabled methods with more parameters than the request are removed
  3. If allowExtraParams is disabled then methods with less parameters than the request are removed
  4. If either of the two parameters above are enabled then methods with the lowest difference in parameter count from the request are kept
  5. Parameters types are compared to the request parameters and the method(s) with the highest number of matching parameters is kept
  6. If there are multiple methods remaining then the first of them are used

jsonrpc4j's method resolution allows for overloaded methods sometimes. Primitives are easily resolved from json to java. But resolution between other objects are not possible.

For example, the following overloaded methods will work just fine:

json request:

{"jsonrpc":"2.0", "id":"10", "method":"aMethod", "params":["Test"]}

java methods:

public void aMethod(String param1);
public void aMethod(String param1, String param2);
public void aMethod(String param1, String param2, int param3);

But the following will not:

json request:

{"jsonrpc":"2.0", "id":"10", "method":"addFriend", "params":[{"username":"example", "firstName":"John"}]}

java methods:

public void addFriend(UserObject userObject);
public void addFriend(UserObjectEx userObjectEx);

The reason being that there is no efficient way for the server to determine the difference in the json between the UserObject and UserObjectEx pojos.

Custom method names

In some instances, you may need to expose a JsonRpc method that is not a valid Java method name. In this case, use the annotation @JsonRpcMethod on the service method.

@JsonRpcService("/jsonrpc")
public interface LibraryService {
@JsonRpcMethod("VideoLibrary.GetTVShows")
List<TVShow> fetchTVShows(@JsonRpcParam("properties") final List<String> properties);
}
{"jsonrpc":"2.0", "method": "VideoLibrary.GetTVShows", "params": { "properties": ["title"] 

SON-RPC for Java的更多相关文章

  1. (转) RabbitMQ学习之远程过程调用(RPC)(java)

    http://blog.csdn.net/zhu_tianwei/article/details/40887885 在一般使用RabbitMQ做RPC很容易.客户端发送一个请求消息然后服务器回复一个响 ...

  2. java 远程调用 RPC

    1. 概念 RPC,全称为Remote Procedure Call,即远程过程调用,它是一个计算机通信协议.它允许像调用本地服务一样调用远程服务.它可以有不同的实现方式.如RMI(远程方法调用).H ...

  3. Java简单的RPC实现(一)

    RPC使用java最基本的,传输层使用Socket,序列化使用Serializable,java 动态代理模式,但是未实现消息注册等相关信息 大道至简 server端 package com.rpc. ...

  4. 【Other】最近在研究的, Java/Springboot/RPC/JPA等

    我的Springboot框架,欢迎关注: https://github.com/junneyang/common-web-starter Dubbo-大波-服务化框架 dubbo_百度搜索 Dubbo ...

  5. 面试题思考:Java RMI与RPC,JMS的比较

    RPC:(Remote Procedure Call)  被设计为在应用程序间通信的平台中立的方式,它不理会操作系统之间以及语言之间的差异. 支持多语言 RMI:(Remote Method Invo ...

  6. Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法

    最近在学习传智播客吴超老师的Hadoop视频,里面他在讲解RPC通信原理的过程中给了一个RPC的小例子,但是自己编写的过程中遇到一个小错误,整理如下: log4j:WARN No appenders ...

  7. java实现RPC

    一,服务提供者 工程为battercake-provider,项目结构图如下图所示 1.1 先创建一个“卖煎饼”微服务的接口和实现类 package com.jp.service; public in ...

  8. 从RPC开始(一)

    这是一篇关于纯C++RPC框架的文章.所以,我们先看看,我们有什么? 1.一个什么都能干的C++.(前提是,你什么都干了) 2.原始的Socket接口,还是C API.还得自己去二次封装... 3.C ...

  9. RPC远程过程调用学习之路(一):用最原始代码还原PRC框架

    RPC: Remote Procedure Call 远程过程调用,即业务的具体实现不是在自己系统中,需要从其他系统中进行调用实现,所以在系统间进行数据交互时经常使用. rpc的实现方式有很多,可以通 ...

  10. RPC

    那是N年前的一天,老王在看一本讲java的技术书(可惜忘了叫啥名字了),突然看到有一章讲RMI的,立马就觉得很好奇.于是乎,就按书上所讲,写了demo程序.当时也就只知道怎么用,却不知道什么原理.直到 ...

随机推荐

  1. bjfu1238 卡特兰数取余

    题目就是指定n,求卡特兰数Ca(n)%m.求卡特兰数有递推公式.通项公式和近似公式三种,因为要取余,所以近似公式直接无法使用,递推公式我简单试了一下,TLE.所以只能从通项公式入手. Ca(n) = ...

  2. <转>浅谈DNS体系结构:DNS系列之一

    浅谈DNS体系结构 DNS是目前互联网上最不可或缺的服务器之一,每天我们在互联网上冲浪都需要DNS的帮助.DNS服务器能够为我们解析域名,定位电子邮件服务器,找到域中的域控制器……面对这么一个重要的服 ...

  3. char[]数组与char *指针的区别

    char[]数组与char *指针的区别 问题描述 虽然很久之前有看过关于char指针和char数组的区别,但是当时没有系统的整理,到现在频繁遇到,在string,char[], char *中迷失了 ...

  4. linux 用 SSH2协议远程连接并控制 linux

    [参考链接](http://php.net/manual/zh/ssh2.installation.php) ssh2_exec 并不能打印所有的命令的提示信息 如果有返回的字符串信息,可以打印,或重 ...

  5. DOM笔记(三):Element接口和HTMLElement接口

    一.Element接口 Element接口表示一个元素,该接口扩展自Node接口,自然继承了Node接口的属性和方法,也有一套针对元素的属性和方法. Element接口常见的属性比较少,常用的就是一个 ...

  6. Java学习笔记(3)

    “当你定义出一组类的父型时,你可以用子型的任何类来填补任何需要或期待父型的位置” “运用多态时,引用类型可以是实际对象类型的父类”Animal myDog = new Dog(); 三种方法可以防止某 ...

  7. [Hive - LanguageManual] Import/Export

    LanguageManual ImportExport     Skip to end of metadata   Added by Carl Steinbach, last edited by Le ...

  8. Distributed Sentence Similarity Base on Word Mover's Distance

    Algorithm: Refrence from one ICML15 paper: Word Mover's Distance. 1. First use Google's word2vec too ...

  9. 第三百零二天 how can I 坚持

    今天给掌中宝提了几个bug,确实管用,哈哈. 还有就是弟弟买房了,海亮艺术公馆,还好,至少安定下来了,可惜啊,我看好的房子也有的卖了,咋办啊. 看准的东西总是会想法设法的买了,可是无能为力啊. 还有, ...

  10. 软件工程——PairProject

    结对编程组员: 马辰     11061178 柴泽华  11061153 1)    照至少一张照片, 展现两人在一起合作编程的情况. 结对编程的优点 1)在编程过程中,任何一段代码都不断地复审,同 ...