Skip to content

exposedpath

Satish Sharma edited this page Jul 10, 2018 · 1 revision

How to expose a REST endpoint using Magic-Compose

As we are building microservices, and the most common form of interaction between microservices these days are through the REST endpoints. This section we will learn how to expose an endpoint in Magic-Compose.

Note: These elements may vary according to the framework and tool selected for project. Currently these are specific to spring Boot and related frameworks.

!!! warning We assume that your follow common guidelines related to JAVA and those for defining REST endpoints, also do not use blank spaces and special characters in any of vlues for these element using which may cause failure in application generation.

** tags **

!!! note ""

**Possible Value : List/Array of valid java string identifiers**

**tags** in ***Magic-Compose*** are used to group the related exposed endpoints. ***Magic-Code*** will generate a controller for each tagname. And all the ***exposedPath*** elements beloging to a perticular ***tag*** shall be placed in same controller class.
for ex.
    
        "tags": [
            "student",
            "book"
        ]
For above **tags** in ***Magic-Compose*** the ***Magic-Code*** engine will generate two controllers namely ***StudentController*** and ***BookController***

exposedPath

!!! note ""

**Possible Value : List/Array of exposed paths**

This is the parent element for defining exposed endpoints and controllers in ***Magic-Compose***. You will always define your exposed endpoints within this tag.    

** tag **

!!! note "" Possible Value : one of the values define in tags element

This element specifies to which controller this endpoint shall belong to, As ***Magic-Code*** shall generate one controller for each ***tag*** and will put this defined endpoint in the controller matching the ***tag***. 

for ex. if ***tag*** is set to ***student*** then ***Magic-Code*** will look for ***StudentController*** and put the generated code there.

** url **

!!! note "" Possible Value : valid REST url

The value for this element shall be any valid REST URL. This shall be put in **@GET/POST/PUT/DELETE Mapping("<VALUE_HERE>")**. The same shall be exposed as REST endpoint. Please make sure you define all the parameters in ***parameters*** if you have any ***Path Parameter*** in your URL*

** operation **

!!! note "" Possible Value : Valid HTTP operation

This shall contain the valid HTTP operation. And ***Magic-Code*** engine will map the specified URL to the specified HTTP method. Currently we supports following.

    * GET
    * POST
    * PUT
    * DELETE

** operationName **

!!! note "" **Possible Value : valid java method name Follow JAVA guidelines **

This element shall containg valid java method name and ***Magic-Code*** shall create a java method using vlaue specified here.

** parameters **

!!! note "" *Possible Value : Array/List of parameter elements **

This is the parent element to define parameters for a ***exposedPath*** in ***Magic-Compose*** format. You should always define all the parameters you want to have in your endpoint as a child element of this tag
** position **

!!! note "" **Possible Value : HEADER/BODY/PATH **

This specifies the position of the parameter which can have any of the following

* **BODY** : in case you want the parameter to be part of http request body
* **PATH** : set to this if you want the parameter to be a Path Parameter. **Do not miss any parameter which is defined in URL*
* **HEADER** : in case you want the parameter to be passed as the part of http header.
** name **

!!! note "" Possible Value : Valid java parameter identifier

The name of the parameter. And this can be any valid java parameter name
** type **

!!! note "" Possible Value : INT/STRING/ARRAY/OBJECT/NUMBER/DOUBLE/DATE/BOOLEAN

* INT : to declare a field as java **int**
* STRING: to declare as java **String**
* Array: to declare as java **List<ref type>**
* OBJECT: to declare as type of other **object**
* Other : as same valid java type
** format **

!!! note "" Possible Value : ARRAY

Set this to **ARRAY** in case you want the List type. If this is set then **ref** property is mandatory.
ref

!!! warning "Used only is mentioned cases else not used" Possible Value : any defined entity type

This value of this field could be any defined ***entity*** type and its **only used** below cases

* If the value of ***format*** is defined as ***ARRAY***
* If the value of ***datatype*** is defined as ***OBJECT***

** response **

!!! note "" **Possible Value : NA **

This is the parent element to define response of an **exposedPath** in ***Magic-Compose**. This shall contain follwing elements.
** type **

!!! note "" Possible Value : INT/STRING/ARRAY/OBJECT/NUMBER/DOUBLE/DATE/BOOLEAN

Same as above.
** format **

!!! note "" Possible Value : ARRAY

Same as above.
ref

!!! warning "Used only is mentioned cases else not used" Possible Value : any defined entity type

Same as above.

Exposing REST endpoints

REST controllers to be created

JPA Entities

REST endpoints to be exposed

JPA Entities

** Magic-Compose for endpoints **

    "tags": [
        "student",
        "book"
    ],
  "exposedPath": [
    {
      "tag": "student",
      "url": "/getstudent",
      "operation": "GET",
      "operationName": "getStudent",
      "parameters": [
        {
          "position": "HEADER",
          "name": "token",
          "type": "INT",
          "format": "INT32"
        },
        {
          "position": "HEADER",
          "name": "token2",
          "type": "INT",
          "format": "INT32"
        },
        {
          "position": "PATH",
          "name": "rollNumber",
          "type": "INT",
          "format": "INT32"
        },
        {
          "position": "BODY",
          "name": "student",
          "type": "OBJECT",
          "format": "ARRAY",
          "ref": "student"
        }
      ],
      "response": {
        "type": "OBJECT",
        "format": "ARRAY",
        "ref": "student"
      }
    },
    {
      "tag": "student",
      "url": "/addstudent",
      "operation": "POST",
      "operationName": "addStudent",
      "parameters": [
        {
          "position": "HEADER",
          "name": "token",
          "type": "INT",
          "format": "INT32"
        },
        {
          "position": "PATH",
          "name": "rollNumber",
          "type": "INT",
          "format": "INT32"
        },
        {
          "position": "BODY",
          "name": "student",
          "type": "OBJECT",
          "format": "ARRAY",
          "ref": "student"
        }
      ],
      "response": {
        "type": "OBJECT",
        "format": "OBJECT",
        "ref": "Student"
      }
    }
  ]

Clone this wiki locally