10

Reusable REST Microservice Connector Methods

 3 years ago
source link: https://nixmash.com/java/reusable-rest-microservice-connector-methods/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Reusable REST Microservice Connector Methods

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().

rerest0330a.png

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.

rerest0330b.png

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.

rerest0330c.png

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>.

rerest0330d.png

Simplifying the plumbing in Microservice Application Design with Reusable REST Connector Methods!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK