JSON-RPC VS JAX-WS


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/

猜你喜欢

转载自fengjia10.iteye.com/blog/1083541