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 Language Java Spring Boot leave the default (currently 3.2.2) Project Metadata leave the defaults Packaging Jar Java 17 Dependencies Spring Web 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:
Code SnippetCopy codeSwitch to dark mode12cd ~/projects unzip demo.zipOpen the demo project folder by choosing File → Open Folder
Complete the project with a basic controller class.
Create a new file demo/src/main/java/com/example/demo/DemoController.java.
Enter the following code content:
Code SnippetCopy codeSwitch to dark mode123456789101112131415package 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/src/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.Code SnippetCopy codeSwitch to dark mode12cf api YOUR_API_ENDPOINT_GOES_HERE cf login
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 SnippetCopy codeSwitch to dark mode1234567891011121314--- applications: - name: my-spring-demo memory: 1024M timeout: 300 random-route: true buildpacks: - sap_java_buildpack env: JBP_CONFIG_COMPONENTS: '{ "jres": ["com.sap.xs.java.buildpack.jre.SAPMachineJRE"] }' JBP_CONFIG_SAP_MACHINE_JRE: '{ "version": "17.+" }' path: target/demo-0.0.1-SNAPSHOT.jarDeploy the application to cloud foundry by tunning the following command:
At the end of the deployment process, you will get a text, such as the following:Code SnippetCopy codeSwitch to dark mode1cf pushCode SnippetCopy codeSwitch to dark mode12state since cpu memory disk details #0 running 2020-04-24T17:25:03Z 66.4% 167.5M of 1G 152.4M of 1G
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://cockpit.hanatrial.ondemand.com. 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.. → Java. The launch.json file is opened.
Replace the full content of the file with the following:
Code SnippetCopy codeSwitch to dark mode1234567891011121314151617181920{ "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 SnippetCopy codeSwitch to dark mode1234cf set-env my-spring-demo JBP_CONFIG_JAVA_OPTS "[java_opts: '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000']" cf enable-ssh my-spring-demo cf restage my-spring-demo cf ssh -N -T -L 5005:localhost:8000 my-spring-demoIn 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 line 12. 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.
Remove the breakpoint.
Disconnect the debugger.
In the Terminal, chooseCtrl+C to interrupt the cf ssh command.