Create a simpleHello, World application using Java and the Spring-boot framework. Deploy the application to Cloud Foundry. Debug it remotely from Business Application Studio.
Prerequisites
For the complete execution of current exercise, you must execute the following activities first, using SAP Business Application Studio:
- Execute the previous exercise Creating your free trial account in SAP BTP.
- Open SAP Business Application Studio, open the training workspace you created in the previous exercise.
- Choose File → Open Folder and open the
/home/user/projects
folder.
Steps
In the Web browser, access the https://start.spring.io website, then generate and download a Spring Boot application based on the following information:
Setting Value Project Maven Project Language Java Spring Boot latest version 2 (currently 2.7.16) Project Metadata leave the defaults Packaging Jar Java 8 Dependencies Spring Web Note
This step is optional, it explains the necessary steps needed to create theapp2
Java application from scratch using the Spring Initializr platform. In case you are not interested, just skip the step and continue at the following one.In your Web browser, open https://start.spring.io.
Choose the settings as described in the provided table.
Choose Generate. A file named
demo.zip
is downloaded.
Upload and open the generated project to SAP Business Application Studio
In SAP Business Application Studio, , open the
~/projects
folder.Upload the
demo.zip
archive to the~/projects
folder (just drag and drop it).Unzip the archive by running the following commands in the terminal:
Open the
demo
project folder by choosing File → Open Folder
Complete the project with a basic controller class.
Create a new file
demo/main/java/com/example/demo/DemoController.java
.Enter the following code content:
Code snippetExpandpackage com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @GetMapping("/") public String hello() { String page = "Hello Spring!"; return String.format(page); } }
Build the project by running
mvn clean install
in the terminal.Run the program in debug mode.
Open the
demo/main/java/com/example/demo/DemoApplication.java
file.Choose Run → Start Debugging
When the popup message appears A service is listening to port 8080, choose Open in a New Tab.
A new tab is opened, displaying Hello Spring!.
Set a breakpoint at
DemoController.java
line 12 and make the debugger stop there.In the file
DemoController.java
, click aside line 12, a red dot switches on.Refresh the Hello Spring! tab. Program execution pauses at line 12 in the debugger.
Choose Continue (F5). The execution continues.
Remove the breakpoint.
Stop the program execution.
Login to Cloud Foundry
Make sure you have your Cloud Foundry API endpoint, user (email) and password, created in the previous exercise.
Open a terminal and run the following commands:
When requested, enter your email and your password.
Deploy the application to Cloud Foundry.
In the project root folder, create a new deployment descriptor file with name
manifest.yml
with the following content:Being a yaml file, pay attention that proper vertical alignment is preserved. For example, words name, memory, timeout, random-route, need to start at the same horizontal character.Code snippetExpand--- applications: - name: my-spring-demo memory: 1024M timeout: 300 random-route: true path: target/demo-0.0.1-SNAPSHOT.jar buildpacks: - sap_java_buildpack
Deploy the application to cloud foundry by tunning the following command:
Look for the running application in the SAP BTP Cockpit and open it.
In a web browser, open your SAP BTP Cockpit based on the url you received at subscription time (for example: https://emea.cockpit.btp.cloud.sap. Login if required..
Navigate to the training Subaccount.
Navigate to the dev space.
Choose the my-spring-demo application name.
Choose the Application Route. The application opens in the Hello Spring! page.
In SAP Business Application Studio, create a debug configuration to attach the remote Java application in Cloud Foundry.
Choose Run → Add Configuration... The
launch.json
file is opened.Replace the full content of the file with the following:
Code snippetExpand{ "version": "0.2.0", "configurations": [ { "type": "java", "name": "Launch Application", "request": "launch", "mainClass": "com.example.demo.DemoApplication", "projectName": "demo" }, { "type": "java", "name": "Attach Application", "request": "attach", "hostName": "localhost", "port": 5005, "sourcePaths": [ "${workspaceFolder}" ] } ] }
Attach the debugger of SAP Business Application Studio to the remote application running in Cloud Foundry.
To enable the remote application for debug, execute the following commands:
Ignore eventual messages proposing you to expose port 5005.Code snippetExpandcf set-env my-spring-demo JBP_CONFIG_JAVA_OPTS "[java_opts: '-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n']" cf enable-ssh my-spring-demo cf restage my-spring-demo cf ssh -N -T -L 5005:localhost:8000 my-spring-demo
In SAP Business Application Studio, in the Run and Debug tab, run the Attach Application configuration. The debugger attaches to the remote application.
Set a breakpoint in the
DemoController.java
file at line12
. Open the application in the browser and verify that the execution stops at the breakpoint. Continue the execution to the end. Remove the breakpoint. Stop the application.Open the
src/main/java/com/example/demo/DemoController.java
file. Create a breakpoint by selecting aside line number 12. A red dot appears beside the number.Re-open or refresh the the Hello Spring! web page in Cloud Foundry. The program execution pauses in the debugger.
Choose Continue (F5). The execution continues.
In the welcome screen, choose
HelloWorldController
. The controller runs and the execution stops in the debugger, at the breakpoint (switch back to SAP Business Application Studio).In the Debugger, choose Continue.
hello: 'world'
appears in the browser.In the
HelloWorldController.java
, click on the red dot to remove the breakpoint.Remove the breakpoint.
Disconnect the debugger.
In the Terminal, chooseCtrl+C to interrupt the
cf ssh
command.