Writing Java API - shephertz/App42_APIGateway_Docs GitHub Wiki
Writing your API in Java
You can create your own API by writing a server side logic. To create your own API, just download API Sample eclipse sample project and import this in to your eclipse. You will find a sample java file in side it named as com.test.MyAPI. This class implements Executor interface and you can write your logic inside execute method as shown below. Also, you are free to rename this class file based on your requirement.
public class MyAPI implements Executor {
@Override
public HttpResponseObject execute(HttpRequestObject requestObject) {
//Get Request Body
String requestBody = requestObject.getBody();
//Get Header Map
HashMap<String, String> requestHeaders = requestObject.getHeaderMap();
// ########### Write your Logic Below ############//
//................................................//
//................................................//
//................................................//
// ########### Logic End ############//
// Send Your Response.....
HashMap<String, String> responseHeader = new HashMap<String, String>();
responseHeader.put("MyCustomHeader", "XXXXXXX");
int responseStatus = 200; // OK...
String responseMessage = "{'message':'Success/Custom Message'}"; // You can pass your custom message
return new HttpResponseObject(responseStatus, responseMessage , responseHeader);
}
}
Once you have written your logic, you can create jar of this project and deploy it on gateway. You can create jar file using any java jar utility or running maven command (ant jar) from root of this project folder. You can deply this created jar from logging to Gateway console as shown here.