![Spring_Initializer-1.jpg](https://quantinsightsnetwork.com/wp-content/uploads/2023/10/Spring_Initializer-1-678x381.jpg)
š§ 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 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.
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.
Be the first to comment