Create custom REST Resource for GET and POST method in Drupal 8

One of the important features that are inspired by drupal 7 and imported to Drupal 8 with enhancement is its REST API. Drupal provides inbuilt Rest support for exposing entities and creating new entities. You can view all those Rest endpoints by installing the rest_ui module. However, sometimes we also need to define custom Rest Resources / Endpoints for our custom requirement.

So here is how we can define custom Rest Resources.