Skip to Content
Technical Articles
Author's profile photo Subhajit Das

Build and Launch a Simple Spring Boot App(web-service) on SAP BTP

🟧 Introduction:

Welcome to the world of cloud-based application development on SAP Business Technology Platform (BTP). If you think of exploring Application Development in CAP on JAVA stack, you might think of exploring and developing a simple spring boot application first and deploy the same on BTP.

In this blog post, I’ll be showing you how to develop and deploy a simple Spring Boot application on SAP BTP.

Combining the flexibility and simplicity of Spring Boot with the robust capabilities of SAP BTP, you’ll be able to build cutting-edge cloud applications that can scale effortlessly and leverage a wide array of SAP services.

Being an ABAP’er and UI5’er myself, I found this area pretty interesting and hence the motivation.

 

🟦 Getting Started with SAP BTP:

Before we dive into the technical aspects, let’s take a moment to understand what SAP BTP is all about.

SAP Business Technology Platform is a comprehensive platform-as-a-service (PaaS) offering from SAP that enables developers to build, extend, and integrate business applications in the cloud. It is a collection of tools, services, and technologies that work together to support various business operations and digital transformation.

By using SAP BTP, businesses can innovate and adapt quickly to changing market demands, leverage advanced technologies, and accelerate their digital transformation journey. It provides a scalable and flexible foundation for building modern, intelligent, and connected applications that drive business growth and efficiency.

Very well explained in simple terms by colleague’s Paul here & Raja here .

To get started, create a BTP trial account in BTP via

https://account.hana.ondemand.com/#/home/welcome and try accessingĀ  https://cockpit.hanatrial.ondemand.com/trial/#/home/trialĀ thereby.

Also, make sure that Subaccount and Space has been created.

 

🟦 Context:

So we will end up building a simple service accept HTTP GET requests at endpoint /welcome and try running it locally as Well. Then we would proceed and deploy it on BTP.

 

🟦 Preparing Your Development Environment:

To start building your Spring Boot application locally you’ll need to set up your development environment.

Ensure you have the following installed:

  • Java Development Kit (JDK) /Ā SapMachine
  • Maven
  • IDE of your choice locally installed configured to support Spring Boot projects. I will be using VS Code
  • Additionally, for deployment in BTP via CLI, you might want to install CF CLI.

Maven is a build automation tool provided by Apache foundation to help in the build, documentation and dependency process of projectsĀ  written in Java. We would need certain maven commands to build and generate artifacts.

 

🟦 Creating a Spring Boot Application:

With your development environment ready, it’s time to craft your Spring Boot application.

In this section, I’ll guide you through the process of creating a basic Spring Boot application and run it locally.

First, I’ll be using Spring Initializer to scaffold a Spring Boot Maven project.

The Spring Framework (Spring) is an open-source application framework that provides infrastructure support for developing Java applications. Spring Boot is basically an extension of the Spring framework which eliminates the boilerplate configurations and provides Java developers with a platform to get started with an auto configurable production-grade Spring application.

Spring%20Initializer

Spring Initializer

 

Simplifying, I have chosen the below details important:

Project Name as spring-demo

Project Type as Maven

Language as Java

Package name as com.btp.example

Java Version as 17 based on my current configuration

Also from Dependencies, Select the Spring Web dependency as this dependency is required for any web application that uses Spring MVC.

All done. Now Generate the project and you would get a zip file downloaded which is an archive of a web application that is configured with your choices.

Extract and Import the generated project folder in an IDE to get going.

>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

We’ll go ahead and develop a web service that will handle GET request as of now. We shall be querying on endpoint ‘/welcome’ and maybe query will some parameters later on.

The request should return a JSON response as follows:

{ "text": "Hello, Enthusiast" }

 

To model the /welcome representation, we need to create a Resource Representation class attached to this resource.

To do so, let us prepare a simple JAVA class with getter and setter methods for theĀ text data under src/main/java/com/btp/example/springdemo/Welcome.java as following

public class Welcome {
    
    private String name;

    public Welcome(String name)
    {
        this.name = name;
    }

    public String getContent() {
        return name;
    }

}

Representation Class

This will simply help us in getting and setting the text data.

Since the HTTP GET request will be handled by a controller, hence we need to build a Resource Controller class as well.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WelcomeController {

    private static final String strDefine = "Hello, %s!";

    @GetMapping("/welcome")
    public Welcome handleWelcome(@RequestParam(value = "name", defaultValue = "Enthusiast") String name) {
        return new Welcome(String.format(strDefine, name));
    }
}

Here comes the power of annotations in Spring Boot.

In Spring Boot, theĀ controller class is responsible for processing incoming REST API requests, preparing a model, returning the view to be rendered as a response.

Annotations @ControllerĀ or theĀ @RestController annotation marks controller classes as a request handler to allow Spring to recognize it as a RESTful service during runtime.

Because of theĀ @ResponseBodyĀ annotation, the fields from the fetched object are serialized into JSON and returned to the client that requested it.

TheĀ @RestControllerĀ annotation in Spring is essentially just a combination ofĀ @ControllerĀ andĀ @ResponseBody

For us, we would annotate our Welcome Controller class with @RestController annotation which would allow it to handle GETĀ requests forĀ /welcome by returning a new instance of the Welcome class.

TheĀ @GetMappingĀ annotation ensures that HTTP GET requests toĀ /welcome are mapped to the handleWelcome() method. @RequestParam binds the value of the query string parameter text into the nameĀ parameter of theĀ greeting()Ā method

TheĀ main()Ā method uses Spring Boot’sĀ SpringApplication.run()Ā method to launch an application

The returned Welcome will must be converted to JSON by Spring’s HTTP message converter.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Now once we are ready, we can either run this application directly using mvn spring-boot:run on root folder or build a artifact (which in simple words is a .jar file) using mvn clean install and run it using java -jar.

Since we would be deploying to BTP, we would generate a .jar file out of it and test it locally by running it.

So on our root folder I do a mvn clean package/mvn clean install first to compile and build a jar artifact. You can also rename the .jar filename in pom.xml

We should see ‘Build Success’

Build%20Success%20Message

Build Success Message

 

Now, we navigate to the target folder where the jar is located and run the jar using java jar <jar file name>.

In our case it should be under spring-demo\spring-demo\target. You can also rename the jar file from pom.xml

 

Once done, lets try executing this by using java -jar <.jar name>.

Once successful, you should be able to see your Spring Application running at port 8080 now.

Lets try opening http://localhost:8080/welcome

Response

Response

Woo Hoo 😊!!!, our service is up and running..

Lets query it with a custom text lets say http://localhost:8080/welcome?name=’Visitor’

Response%20with%20query

Response with query

That’s it. Let try deploying this very simple application in BTP in next step.

 

🟦 Deploying Your Spring Boot Application on SAP BTP:

After developing your application, it’s time to deploy it on SAP BTP. SAP BTP provides a Cloud Foundry environment for deploying applications with ease. We’ll walk you through the deployment process, ensuring that your app is up and running on the cloud in no time.

We can do this either via CLI or through the interactive BTP Cockpit. Here I would be deploying using CF CLI.

I would be deploying my application in a space created on CF Environment in my created Trial Account.

In order to provide the CF Environment with the necessary information to deploy your application, we would be needing a manifest.yml file for the same.

Thing to note here is mentioning the ‘buildpack‘ and ‘env‘ property as mentioned in the manifest.yml is really necessary as without this you may get a Java runtime error.

Also if you are deploying via the BTP Cockpit on the web, make sure to maintain the properties ‘JBP_CONFIG_OPEN_JDK_JRE’ and ‘JBP_CONFIG_SPRING_AUTO_RECONFIGURATION’ with their respective values(as mentioned in the manifest.yml) under User-Provided Variables option on the left pane after you click on your application instance.

Ok now let us login ad connect to our BTP account via Cf CLI.

In a new terminal, type cf login and enter your email and password.

I prefer using –sso (cf login –sso) with an addition as it helps in generating a temporary authentication code.

Copy the authentication code and in terminal. do a single right click to paste it.

Once authenticated it would show you the connected account and dev spaces.

Choose the required dev space and enter cf push.

Now you should get something like this once your application is deployed.

Lets go the BTP Cockpit and check the same.

You can see the application running and access the endpoint from here.

Cockpit

Cockpit

 

Response

Response

 

Congratulations, your Spring Boot application is now live on SAP BTP.

Source code can be foundĀ here

 

🟧 Conclusion:

By now, you might have got familiar with developing and deploying a simple Spring Boot application on SAP BTP.

The combination of Spring Boot’s developer-friendly approach and SAP BTP’s robust services opens up endless possibilities for building innovative cloud applications.

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Peter Ondruska
      Peter Ondruska

      Does not work. I cannot install CL cli to deploy and deployed from Cockpit. The application fails to start with:

      2 Oct 2023, 00:28:37 (GMT+02:00) - [APP/PROC/WEB/0]

      Exception in thread "main" java.lang.UnsupportedClassVersionError: org/springframework/boot/loader/JarLauncher has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
      Clearly, the environment is running with an older Java, not 17 as described. Cannot find a way to change that.
      Author's profile photo Peter Ondruska
      Peter Ondruska

      OK, reviewed the source code and the important part for deployment is the manifest file (manifest.yml). Even though I modified it (name and path) it still fails to start.

      Author's profile photo Peter Ondruska
      Peter Ondruska

      Installed CF CLI and deployed using command line worked fine. Deployment from BTP web does not work.

      Author's profile photo Subhajit Das
      Subhajit Das
      Blog Post Author

      Hey Peter,

      That's correct and hence I chose the CF CLI.

      Surprisingly BTP Cockpit gives that error.

      The reason to maintain those values is that in absence of that, while deploying Java 8 is expected.

      If you are trying to deploy via Cockpit, try maintaining the JBP property and values under User-Provided Variables in Cockpit and see if that works(re-deploy).

       

      Regards,

      Subhajit

      Author's profile photo Peter Ondruska
      Peter Ondruska

      Dear Suhajit,

      I managed already using CLI but there must be something wrong (maybe just in trials) and BTP Cockpit does not correctly pass manifest. Manifest that works with CLI does not work in BTP Cockpit. Posted this to let others spare time and use CLI.

      Regards,

      Peter