DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above
  • Aggregating REST APIs Calls Using Apache Camel
  • Choosing a Library to Build a REST API in Java

Trending

  • Fact-Checking LLM Outputs Programmatically: Building a Verification Layer That Catches Hallucinations
  • Edge Computing in Utility IoT: Two Architecture Patterns That Actually Work
  • 8 RAG Patterns You Should Stop Ignoring
  • Java Backend Development in the Era of Kubernetes and Docker
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Is Java Remote Procedure Call Dead in the REST Age?

Is Java Remote Procedure Call Dead in the REST Age?

JSON-RPC excels at simple tasks when mapping business concepts to resources via REST is too involved.

By 
Lieven Doclo user avatar
Lieven Doclo
·
Oct. 12, 15 · Opinion
Likes (12)
Comment
Save
Tweet
Share
35.0K Views

Join the DZone community and get the full member experience.

Join For Free

When you’re writing webservices nowadays, you can be sure without a doubt that REST will be your first choice and probably your only choice. Sometimes you just need to quickly build something RPC-like that can be invoked with a simple HTTP call and uses JSON like all the cool kids on the block. Enter JSON-RPC.

JSON-RPC

RPC really got a bad name thanks to some of the standards that were used to achieve it. Most developers shudder when confronted with WSDLs and SOAP envelopes. However, RPC still has a lot of use cases, for example for remoting between a business front-end and a dedicated back-end. Most Spring users are familiar with the remoting features it provides, including HTTP invocation (using Java serialization), JMS and plain old RMI. JSON-RPC can be a very nice drop-in replacement in these circumstances and provide an easy way to test and invoke your API’s using only a browser.

JSON-RPC is an official standard, now in its 2.0 version. It uses JSON payloads to define both the request and the response of the RPC call. A standard JSON-RPC call looks like this:

{
  "id":1234,
  "method":"myRpcMethod",
  "params":["test"]
}

With JSON-RPC you can choose to have a dedicated endpoint per service or a single endpoint, differentiating between the services on the server level by prefixing your method name with a service identifier.

The response will either be a result of the call or a structure returning information of the error in case the call fails.

Using JSON-RPC with Java

There are a couple of JSON-RPC libraries out there. However, as I’ve found out, only one is actually worth looking at, especially if you’re using Spring: jsonrpc4j. It uses Jackson to provide the mapping between POJOs and JSON and can therefor be easily extended to support a plethora of Java libraries such as serialization support for Joda Time and the new Money API.

With jsonrpc4j exposing a service as a JSON service is dead easy. I’m going to give you the basic setup to expose a service in Spring Boot, but you can find more information in the documentation of the project.

For example, say we have a service that needs to be exposed that looks like this:

public interface MyService {
  String sayHelloWorld(String name);
}

public class MyServiceImpl implements MyService {
  public String sayHelloWorld(String name) {
    return "Hello world, " + name;
  }
}

To expose this service to JSON-RPC with Spring Boot, this is the config you need with jsonrpc4j:

@SpringBootApplication
public class RpcApplication {
  public static void main(String[] args) {
    SpringApplication.run(RpcApplication.class);
  }

  @Bean
  public MyService myService() {
    return new MyServiceImpl();
  }

  @Bean(name = "/rpc/myservice")
  public JsonServiceExporter jsonServiceExporter() {
    JsonServiceExporter exporter = new JsonServiceExporter();
    exporter.setService(myService());
    exporter.setServiceInterface(MyService.class);
    return exporter;
  }
}

And that’s it. Start up your Boot application and execute the following curl command:

curl -v -X POST -d '{"id":0, "method":"sayHelloWorld", "params":["John Doe"]}' http://localhost:8080/rpc/myservice

You should then receive the following response:

{"response": "Hello world, John Doe"}

And that’s it for exposing JSON-RPC methods with Spring Boot. Very simple, very quick and very powerful. Personally I like JSON-RPC APIs a lot for internal usage because it has such a small learning curve. While it’s definitely not REST, it does allow you to quickly expose a service over an HTTP interface with JSON datastructures. JSON-RPC APIs can be an excellent addition to your REST APIs. There’s no doubt that REST APIs are preferred for external web services, but for internal communication or internal APIs, JSON-RPC can provide a quick alternative to externalize services without having to worry about mapping everything to RESTful resources.

REST Web Protocols JSON-RPC Java (programming language) remote

Published at DZone with permission of Lieven Doclo. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • How to Consume REST Web Service (GET/POST) in Java 11 or Above
  • Aggregating REST APIs Calls Using Apache Camel
  • Choosing a Library to Build a REST API in Java

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook