Monday, 1 July 2019

Katharsis (JsonAPI)

Katharsis

What’s katharsis? 
Elegant and powerful HATEOAS for Java based on JSON API standard. (from http://katharsis.io/)

What’s HATEOAS?
HATEOAS stands for Hypermedia as the Engine of Application State. It’s a very common concept in browser which we use daily without knowing. HATEOS is, Using Hyperlinks to change the state of an application. For example, our browser will load a web page with plenty of hyperlinks. When you click on the hyperlink it will take us to different webpage or change the state of the same web page. Yes, it’s that much simple.

Coming Back to Katharsis:
Katharsis is a JSON api standard using this HATEOAS concept, that will give you a response along with link you want to use further. And this also enforce the developers to follow the standards in rest url and rest methods. Let’s see an example json,

{
  "data": [
    {
      "id": "1",
      "type": "students",
      "attributes": {
        "student-name": "Arun",
        "gender": "M"
      },
      "relationships": {
        "courses": {
          "data": [
            {
              "id": "1",
              "type": "courses"
            },
            {
              "id": "2",
              "type": "courses"
            }
          ],
          "links": {
            "self": "http://localhost:8080/api/students/1/relationships/courses",
            "related": "http://localhost:8080/api/students/1/courses"
          }
        }
      },
      "links": {
        "self": "http://localhost:8080/api/students/1"
      }
    },
    {
      "id": "2",
      "type": "students",
      "attributes": {
        "student-name": "Ashok",
        "gender": "M"
      },
      "relationships": {
        "courses": {
          "data": [
            {
              "id": "1",
              "type": "courses"
            },
            {
              "id": "2",
              "type": "courses"
            }
          ],
          "links": {
            "self": "http://localhost:8080/api/students/2/relationships/courses",
            "related": "http://localhost:8080/api/students/2/courses"
          }
        }
      },
      "links": {
        "self": "http://localhost:8080/api/students/2"
      }
    }
  ],
  "included": [
    {
      "id": "1",
      "type": "courses",
      "attributes": {
        "course-name": "AWS"
      },
      "links": {
        "self": "http://localhost:8080/api/courses/1"
      }
    },
    {
      "id": "2",
      "type": "courses",
      "attributes": {
        "course-name": "Data Science"
      },
      "links": {
        "self": "http://localhost:8080/api/courses/2"
      }
    }
  ]
}

Please click here for the katharsis example application.