Skip to content

Create REST API for the CDF converter related to SourceMeter #447

@premissa

Description

@premissa

Currently, the CodeMetropolis application and the converter module work as command-line tools. Modern applications are moving towards the microservice architecture. Microservices are an architectural approach to developing software applications as a collection of small, independent services communicating with each other over a network. Instead of building a monolithic application where all the functionality is tightly integrated into a single codebase, microservices break down the application into smaller, loosely coupled services.

The first step to creating microservices is to access the functionality via service endpoints. In this case, those endpoints will be made based on the REST API concept.

To accomplish this task:

  1. Understand the execution steps of the converter.
  • Focus on SourceMeter solutions!
  1. Break down the process into service levels. A service refers to a single operation. For example, you want to define three services: one for passing parameters, one for starting the conversion, and one for retrieving results.
  2. Create a resource class for the REST API. The API is defined for those services you identified in step 2. This class will handle the HTTP requests and map them to appropriate methods. You can use the following packages:
  • javax.ws.rs.*
  • javax.ws.rs.core.MediaType
  • javax.rs.core.Reponse
  1. Create an application configuration class to register your resource class.
    You can use the following packages:
  • javax.ws.rs.ApplicationPath*
  • javax.ws.rs.core.Application
  1. Add the following dependencies to your pom.xml
<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.34</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.34</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.34</version>
    </dependency>
</dependencies>
  1. Create web.xml in the WEB-INF directory to configure the servlet
<web-app>
    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.demo.AppConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>
  1. Deploy your application to a servlet container like Apache Tomcat.
  2. Document the solution.
  3. Test the solution.

The solution is acceptable only if the provided code works and the functions are available via the http/HTTPS protocol, and the result of the run is equivalent to the CLI. The proposed solution must provide the same functionality as the CLI version. It'd be important to take care that handling the error provides a secure message to the caller (for example, denoting the missing parameters). Still, logging must reveal the whole error trace. The solution must be documented, and the architecture of the new service must be presented in a proper diagram. Integration tests using the endpoints must also be provided.

For more information about the Java API for RESTFul Web Services, see https://www.oracle.com/technical-resources/articles/java/jax-rs.html.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions