Skip to content

WebConnector: REST Mapping

Jasper Nalbach edited this page Apr 5, 2016 · 5 revisions

The WebConnector uses the REST Mapper to map service method to a REST api. As a service developer you have basically two ways of specifying a mapping for REST requests:

  1. Use Annotations
  2. Create a XML file manually

The former is usually recommended, since it allows you to design the services in a RESTful way right ahead. But sometimes the code of a service is not accessible or cannot be changed, so it must be made into a RESTful service (or at least the needed parts of it).

The Web Connector regularly checks on a shared service list, where las2peer services can register itself. It then looks for unknown services (from which no mapping data is already stored) and tries to invoke their getRESTMapping method, which is expected to return a XML containing all mapping information. If no such method exists, the service is not processed further.

Annotations

If you create the service by yourself using annotations, you can make the method look like this:

public String getRESTMapping()
{
    String result="";
    try
    {
        result= RESTMapper.getMethodsAsXML(this.getClass());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return result;
}

XML

If you want to make another service compatible, you can provide the mapping data by a compatibility service. This service does not return its own mapping, but instead some other service's mapping, whenever getRESTMapping is invoked.

public String getRESTMapping()
{
    String result="";
    try
    {
        result= RESTMapper.mergeXMLs(RESTMapper.readAllXMLFromDir("./XMLCompatibility2"));
    }
    catch (Exception e)
    {

        e.printStackTrace();
    }
    return result;
}

In this case the service loads all XML files inside a local directory and uploads them to the Web Connector. You can upload multiple XMLs at once, if needed. The Web Connector does not care, where the mappings come from.

Clone this wiki locally