1. Add Dependency
We use the jsonrpc4j library, the latest version is 1.5.3 https://github.com/briandilley/jsonrpc4j1 2 3 4 5 | < dependency > < groupid >com.github.briandilley.jsonrpc4j</ groupId > < artifactid >jsonrpc4j</ artifactId > < version >1.5.3</ version > </ dependency > |
2. Create AutoJsonRpcServiceImplExporter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImplExporter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ApplicationConfig { @Bean public static AutoJsonRpcServiceImplExporter autoJsonRpcServiceImplExporter() { AutoJsonRpcServiceImplExporter exp = new AutoJsonRpcServiceImplExporter(); //in here you can provide custom HTTP status code providers etc. eg: //exp.setHttpStatusCodeProvider(); //exp.setErrorResolver(); return exp; } } |
3. Create ServerAPI interface
1 2 3 4 5 6 7 8 9 10 11 12 13 | import com.googlecode.jsonrpc4j.JsonRpcParam; import com.googlecode.jsonrpc4j.JsonRpcService; import java.util.List; @JsonRpcService ( "/api" ) public interface ServerAPI { long multiplier( @JsonRpcParam (value = "a" ) long a, @JsonRpcParam (value = "b" ) long b); String printPerson( @JsonRpcParam (value = "person" ) Person person); String printPersons( @JsonRpcParam (value = "persons" ) List<person> persons); } |
4. Create ServerAPI implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl; import java.util.List; import org.springframework.stereotype.Service; @Service @AutoJsonRpcServiceImpl public class ServerAPIImpl implements ServerAPI { @Override public long multiplier( long a, long b) { return a * b; } @Override public String printPerson(Person person) { return person.toString(); } @Override public String printPersons(List<person> persons) { return persons.toString(); } } |
5. Deploy to glassfish server
6. Use Postman to test API
6.1. Test 'multiplier' method
Send POST request with 'application/json' to endpoint 'http://localhost:8080/jsonrpc4j/api'1 | {"id":"1","jsonrpc":"2.0","method":"multiplier","params":{"a":1111111,"b":11111}} |
6.2. Test 'printPerson' method
1 | {"id":"1","jsonrpc":"2.0","method":"printPerson","params":{"person":{"name":"Solar City","yearOfBirth":2000}}} |
6.3. Test 'printPersons' method
Send POST request with 'application/json' to endpoint 'http://localhost:8080/jsonrpc4j/api'1 | {"id":"1","jsonrpc":"2.0","method":"printPersons","params":{"persons":[{"name":"Solar","yearOfBirth":1980},{"name":"City","yearOfBirth":1988}]}} |
Download Source Code
7. Implement your own JSON RCP Client
It is quite simple to write a controller to invoke JSON-RPC API by using JsonRpcRestClient1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @RequestMapping (value = "multiplier" ) @ResponseBody public Object getMultiplierResult( @RequestParam ( "a" ) long a, @RequestParam ( "b" ) long b) { try { URL url = new URL(getAPIUrl()); JsonRpcRestClient client = new JsonRpcRestClient(url); Map<String, Object> params = new HashMap<>(); params.put( "a" , a); params.put( "b" , b); Long result = client.invoke( "multiplier" , params, Long. class ); return String.valueOf(result); } catch (Throwable t) { logger.log(Level.SEVERE, "Error while invoking multiplier API" , t); } return null ; } |
8. Deploy JSON RCP Client and Test
8.1. Deploy JSON RPC Client
'SpringJsonRPCClient' can be deployed in the same Glassfish with package 'SpringJsonRPCExample'
8.2. Test JSON RPC Client
Enter the appropriated urls to web browser to invoke API
http://localhost:8080//jsonrpc4j-client/multiplier?a=1111111&b=1111111

http://localhost:8080//jsonrpc4j-client/printperson

http://localhost:8080//jsonrpc4j-client/printpersons

No comments :
Post a Comment