Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
subhajit
Active Participant
5,023

🟧 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 blasius18 here & rajaprasad.gupta 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 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 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


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

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


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


 


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.
6 Comments
Labels in this area