Creating a basic handler class

Each REST API must have an associated handler class that performs the real work of the API. The handler class must contain one method for each HTTP operation that the Swagger schema specifies. The method name must match the operationId value of the associated HTTP operation. The method arguments, if any, must be one of the following:
  • The method arguments must match the list of parameters defined for the HTTP operation.
  • The method arguments must be of type RequestContext.

For example, the Activities schema defines a GET operation on the /activities endpoint with an operationId value of getActivities. Thus, your handler class must contain a method named getActivities.

Your initial version of the handler class must contain methods for each of the following operations. These operations match those of the example Swagger schema listed in Creating the Activity API Swagger schema.
Operation Endpoint OperationId Class method
GET /activities getActivities getActivities()
GET /activities/{activityId} getActivity getActivity(activityId)
POST /activities createActivity createActivity(body)

Example Activity API handler class

For the example Activities API, you need to create a file named ExampleActivityApiHandler.gosu in the following Guidewire Studio directory:
  • gsrc > mc > activityapi
The initial code is a place-holder API handler class for the Activities API. The class method bodies contain no real functionality at this point. However, it is necessary to have the stub handler class in place in order to publish and test your work so far.
package mc.activityapi
 
uses gw.api.json.JsonObject
 
class ExampleActivitiesApiHandler {
 
  function getActivities() : List<JsonObject> {
    print(">> getActivities called")
    return new ArrayList<JsonObject>()
  }
 
  function createActivity(body : JsonObject) : JsonObject {
    print(">> createActivity called")
    print(">> body is:\n" + body.toPrettyJsonString())
    return new JsonObject()
  }
 
  function getActivity(activityId : String) : JsonObject {
    print(">> getActivity called")
    print(">> activityId: " + activityId)
    return new JsonObject()
  }
}
In this example, each stub method does the following:
  • It prints out the input parameters to the method to the console.
  • It returns an empty response of an appropriate type as specified by the JSON response schema.

Specifying the API handler class in Swagger schema

You must associate your handler class with the Activities API. You do this in the Swagger schema, by adding a value for x-gw-apihandler to the schema, for example:
x-gw-apihandlers:
  mc.activityapi.ExampleActivitiesApiHandler
Thus, file activityAPI-1.0.swagger.yaml must contain an entry for x-gw-apihandler similar to the following:
swagger: '2.0'
info:
  description: "APIs for manipulating activities"
  version: '10.0'
  title: "Activities API"
basePath: /mc/activityapi
x-gw-schema-import:
  activities : mc.activityapi.activityAPI-1.0
x-gw-apihandlers:
- mc.activityapi.ExampleActivitiesApiHandler
...

Next steps

Recompile the configuration code and stop and restart the application server for PolicyCenter to pick up these changes.