JSON-RPC VS JAX-WS

本文对比了JSON-RPC与JAX-WS两种Web服务解决方案。JSON-RPC因其实现简单、易于理解而受到青睐。文章通过具体示例展示了如何使用JSON-RPC进行远程过程调用,并说明了其相较于JAX-WS的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


What is the best web service solution? JAX-WS ? Sorry, I couldn't agree with you.

First JAX-WS is a complicate solution. It Includes:
How to exchange XML data? Document or RPC
How to exchange Document data? Wrapped or Bare
How to invoke RPC? Encoding or Literal

How to serialise Java objects to XML? JAXB
How to define a web service ? WSDL, Service, Port, PortType, Operation, Input, Output Messsage...

How about a RPC parameter? IN, OUT or IN/OUT

What a mess?

How about JSON-RPC ? Pretty simple.

How to exchange data? JSON format remote procedure call.
For example: this is a RPC invocation for a remote procedure named "substract",

int subtract(int first, int sec);

The request and response as following:
--> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}
<-- {"jsonrpc": "2.0", "result": 19, "id": 1}

Somebody maybe not convinced? They told me, I have tools to help me.
What do you mean the tools? Java2WSDL, WSDL2Java ? Actually, It is still hard works.

How about JSON-RPC ?
Let us have a look... with proxy tech, we can easy make a json-rpc call. For example:

First, we define a service interface:


public interface AccountService{
    Account getAccount(String name);
    String getName(Long id);
    Long getId(String name);
    Integer getAge(String name);
    Double getBalance(Account account);
    Collection<Account> getAccounts();
}

 

Second, we call the service with a proxy, that is all. cheers.

ServiceProxy proxy = new ServiceProxy(AccountService.class, "http://localhost:8080/jsonrpc/rpc");

accountService = (AccountService)proxy.create();
Account a = accountService.getAccount("jay");
assertNotNull(a);
String name = accountService.getName(1L);
System.out.println("name -> "+ name);
assertNotNull(name);
Collection<Account> accounts = accountService.getAccounts();
System.out.println("accounts -> " + accounts);
assertTrue(accounts.size() > 0);

 

 

How about Ajax?
Do you know how to call JAX-WS with javascript ? Sorry, I have no idea about it....
How about call a JSON-RPC service by javascript ? No problem...

libjsonrpc is an open source apache 2.0 licensed json-rpc 2.0 java implementation. There are tutorials on how to build a json-rpc client/service/ajax. I swear you are able to learn it in half hour.
have a fun with it.


http://code.google.com/p/libjsonrpc/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值