Try OpenEdge Now
skip to main content
Configuration
Configuring third-party Web applications : OpenEdge Management REST API framework : REST API code example
 

REST API code example

An example of the code used in the samplwebapp.war file shipped with OpenEdge Management is provided in Table 5:
Table 125. RestExample.java
 
package com.progress.fathom.sample.webapp;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.progress.fathom.Body;
import com.progress.fathom.Description;
import com.progress.fathom.FathomAPIServletHandler;
import com.progress.fathom.PathParam;
import com.progress.fathom.QueryParam;
import com.progress.fathom.RequestException;
import com.progress.fathom.RequestMapping;
import com.progress.fathom.RequestMethod;
import com.progress.fathom.RequiredResourcePermission;
import com.progress.fathom.Since;
import com.progress.isq.ipqos.resources.security.SecurityBean;
/**
 * this is sample REST API handler based on the OpenEdge Management REST
 * annotation mapping
 *
 */
public class RestExample extends FathomAPIServletHandler {
    @Description("Example REST get request")
    @RequestMapping(path = "/get/{id}", method = RequestMethod.GET)
    @Since("11.7")
    @RequiredResourcePermission(grantName = SecurityBean.SECURITY_ROOT, action = SecurityBean.ACTION_READ)
    public Object getSample(@PathParam("id") String id, @QueryParam("q") String queryString) throws RequestException {
        // object mapper is from jackson JSON library
        ObjectMapper mapper = new ObjectMapper();
        // this creates a simple JSON response using the values from the URL
        // mapped back into the object as JSON object properties
        ObjectNode response = mapper.createObjectNode();
        response.put("id", id);
        return response;
    }
    @Description("Example REST POST request")
    @RequestMapping(path = "/post/{id}", method = RequestMethod.POST)
    @Since("11.7")
    @RequiredResourcePermission(grantName = SecurityBean.SECURITY_ROOT, action = SecurityBean.ACTION_READ)
    public Object postSample(@PathParam("id") String id, @Body ObjectNode body) throws RequestException {
        // object mapper is from jackson JSON library
        ObjectMapper mapper = new ObjectMapper();
        // this creates a simple JSON response using the values from the URL
        // mapped back into the object as JSON object properties, along with the
        // content of the request body
        // from the POST
        ObjectNode response = mapper.createObjectNode();
        response.put("id", id);
        response.put("request", body);
        return response;
    }
}
The REST API framework scans each class for methods that are public and have a RestMapping annotation defined. The annotation specifies the URI pattern and HTTP method that are to be handled. The RequestMapping annotation with a specified path parameter is the least required annotation. If the method parameter is not specified, the annotation considers HTTP GET as the default method parameter.
Note: Each class can contain as many annotated methods as required. Each method must handle only one combination of URI pattern and HTTP method.