I just blogged about Reusable RabbitMQ Microservice Connector Methods, so it's a good time to look at Reusable REST Microservice Connector Methods. REST Connector Methods are methods where a Client Module sends or requests data from a REST Microservice Resource Endpoint, where the two Modules connect. In NixMash Blog Microservices, the Client is the Web Module, with the REST Microservice Resource being the Post Microservice.
We can make these methods reusable, or generic, because they follow the basic principal of populating a Java POJO from a REST Resource as in these examples below. The post you're reading, in fact, was retrieved from Post Microservice with the getPostByName() method using a Reusable REST Connector Method, which we'll see in a minute. The getPostMetaById() method at bottom retrieves the page's Twitter and other metadata you see if you do a View Source in your browser. Both use the same REST Connector Method getRestObject().
Reusable Method to Retrieve and Populate a Java POJO
As I mentioned, the process of populating a Java POJO from a REST Endpoint is a consistent process. We know that the data from the REST Microservice is going to be sent to us as JSON, which we will then convert to our POJO Class Type.
Here is our Generic Reusable REST Connector Method. We are constructing a Class Type from JSON using the Jersey Mapper and casting that object to our Generic Class Type.
A Reusable REST Connector Method for Lists
We can build Reusable Methods for retrieving List<?> Types as well. First, an example of using such a method, here to retrieve the most recent NixMash Posts displayed on the Home Page. Our Reusable Method is getRestList() which we cast to a List<Post> Type.
The key to our Reusable REST Connector Method for retrieving lists is the Jersey constructCollectionType() method circled below where we create a List of Type <? extends Object>.
Simplifying the plumbing in Microservice Application Design with Reusable REST Connector Methods!