Skip to Content
Technical Articles
Author's profile photo Edrilan Berisha

Building an OData Service with a Spring-Boot Java Application using Olingo – Part I

This blog will deal with an end to end implementation of an OData service using the Olingo library for a Java Spring-Boot application. There are some outdated blog posts that explain how to use Olingo, but none of them also uses Spring, or if they do, they do not explain how to add custom logic for some pre- or post-processing for different OData requests.

So, in this first blog post, we will create a java application with a spring-boot framework. It will provide a health check as a REST API in the end.

We start creating our java application with an “Application.java” class in our source folder where the Spring runtime is initialized.

So, for this example we have the following folder structure:

The Application class will only contain the following coding:

package com.github.olingo.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

Now we add the following pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.ecasio.olingo.spring.example</groupId>
    <artifactId>example-olingo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>example-olingo</name>
    <description>Example Olingo Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-core</artifactId>
            <version>2.0.11</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.ws.rs</groupId>
                    <artifactId>javax.ws.rs-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-jpa-processor-core</artifactId>
            <version>2.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.olingo</groupId>
            <artifactId>olingo-odata2-jpa-processor-ref</artifactId>
            <version>2.0.11</version>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.persistence</groupId>
                    <artifactId>eclipselink</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
    </dependencies>


    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>local</activatedProperties>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>com.h2database</groupId>
                    <artifactId>h2</artifactId>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>cloud</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <activatedProperties>cloud</activatedProperties>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

For the health check, we create a controller folder where we create a new Java class, namely HealthCheckController.java .

It contains the following coding:

package com.github.olingo.example.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/health")
public class HealthCheckController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @GetMapping
    public void healthCheck() {
        logger.info("Health check called");
    }

}

With the @RequestMapping(“/api/v1/health”) annotation, we define our REST endpoint for our health check. With @GetMapping, we also define that this endpoint is accessible for GET requests. So far, no rocket science.

Now, we can build this application locally with the help of Maven by typing the following command into the terminal: ‘mvn spring-boot:run’.

Open Postman and call the “localhost:8080/api/v1/health” url which will return a 200 HTTP response.

 

So, we have successfully built our first REST endpoint in our Spring application.

In the next blog post, we will introduce our entity relation model and build the OData service using the Olingo framework.

For those who need the complete solution, you will find it here: Github repo with solution branch (it’s on the addHealthCheck branch of the repository)

 

 

Best,

Edrilan Berisha

SAP S/4HANA Cloud Development Financials

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Robert Zieschang
      Robert Zieschang

      Hi Edrilan Berisha,

      looking forward for the next parts. Will you also be covering implementing an oData client with Olingo?

      Best,
      Robert

      Author's profile photo Edrilan Berisha
      Edrilan Berisha
      Blog Post Author

      Hi Robert Zieschang,

      stay tuned I will have to see how much time I have, so maybe I end up showing how to implement it on client side.

       

      Best,

      Edrilan Berisha

      SAP S/4HANA Cloud Development Financials

      Author's profile photo coder zzc
      coder zzc

      Thank you very much for your three articles. I am a beginner in OData. Your articles are very helpful to me. Thank you

      Author's profile photo Saransh Vashistha
      Saransh Vashistha

      Thank you so much for these articles, this gave me a good starting point