Spring Boot is a framework provided by the Pivotal team to simplify the initial build and development of new Spring applications. It uses specific configuration methods, so you don't need to define template-based configuration items.
This document describes how to use Spring Boot through SCF to build a todo application. SCF provides event-triggered functions and HTTP-triggered functions (recommended in Spring Boot scenarios).
You have prepared the development environment and tools as instructed in Notes on Java.
SCF provides template functions. You can use an HTTP-triggered function as follows to quickly create a todo application and add, delete, modify, and query todos.
Note:This template is for demonstration only. Todo data is actually stored in the instance cache instead of being persistently stored.
springboot
and webfunc
. In the search results, select SpringBootToDoApplication and click Next as shown below:On the Function Code tab, follow the steps below to initiate a simulated request based on the test template. In this way, you can try out the CRUD features of the todo application.
Query the todo list:
Select GET as the request method, enter /todos
for path
, click Test, and you will see the current todos in the response body as shown below:
Add a todo:
Select POST as the request method, enter /todos
for path
and {"key":"3","content":"Third todo","done": false}
for body
, and click Test to add a todo as shown below:
Delete a todo:
Select DELETE as the request method, enter /todos/2
for path
(to delete the todo whose key
is 2 for example), and click Test as shown below:
Modify a todo:
Select PUT as the request method, enter /todos/3
for path
(to mark the todo whose key
is 3 as completed for example), enter {"key":"3","content":"Third todo","done": true}
for body
, and click Test as shown below:
In the Creating function step, you can also modify the function template based on your business needs. On the Select Template page, click Learn More in the top-right corner of the template card. On the pop-up page, click Download Template Function to get the template function source code.
You can migrate a native Spring Boot project to an HTTP-triggered function as follows:
Make sure that the Spring listening port is 9000 (specified listening port of the SCF HTTP-triggered function).
Compile the JAR package.
Download the code and run the following compilation command in the Webfunc-Java8-SpringBoot
directory:
gradle build
After the compilation, you can get the JAR package in the build/libs
directory. Select the JAR package whose suffix is -all
.
Prepare the executable file scf_bootstrap
to start the web server. Below is the sample file content:
#!/bin/bash
/var/lang/java8/bin/java -Dserver.port=9000 -jar scf-springboot-java8-0.0.2-SNAPSHOT-all.jar
Note:Run
chmod 755 scf_bootstrap
in the directory of thescf_bootstrap
file to ensure that the file has the execution permission.
scf_bootstrap
file and the generated JAR package into a ZIP package and deploy it to SCF as follows:SCF provides template functions. You can use an event-triggered function as follows to quickly create a todo application and add, delete, modify, and query todos.
Note:This template is for demonstration only. Todo data is actually stored in the instance cache instead of being persistently stored.
springboot
. In the search results, select SpringBoot and click Next.Note:If you have created an API Gateway trigger during function creation, simply check whether its configuration is the same as that described below.
/todos
, click Complete Now, and release the service as prompted, as shown below:On the Function Code tab, follow the steps below to initiate a simulated request based on the test template. In this way, you can try out the CRUD features of the todo application.
Query the todo list:
Select GET as the request method, enter /todos
for path
, click Test, and you will see the current todos in the response body as shown below:
Add a todo:
Select POST as the request method, enter /todos
for path
and {"key":"3","content":"Third todo","done": false}
for body
, and click Test to add a todo as shown below:
Delete a todo:
Select DELETE as the request method, enter /todos/2
for path
(to delete the todo whose key
is 2 for example), and click Test as shown below:
Modify a todo:
Select PUT as the request method, enter /todos/2
for path
(to mark the todo whose key
is 2 as completed for example), enter {"key":"2","content":"Third todo","done": true}
for body
, and click Test as shown below:
In the Creating function step, you can also modify the function template based on your business needs. On the Select Template page, click Learn More in the top-right corner of the template card. On the pop-up page, click Download Template Function to get the template function source code.
You can configure as follows:
ScfHandler
class, which is used to receive event triggers and forward messages to the Spring service. After receiving the response from the Spring service, the function will return it to the invoker.ScfHandler
class is added is as shown below:.
├── src
| └── main
| | ├── java
| | | └── com.tencent.scfspringbootjava8
| | | | ├── controller
| | | | ├── model
| | | | └── repository
| | | | | ├── ScfHandler.java
| | | | | └── ScfSpringbootJava8Application.java
| | └── resources
Compile the JAR package
Download the code and run the following compilation command in the root directory:
gradle build
After the compilation, you can get the JAR package in the build/libs
directory. Select the JAR package whose suffix is -all
.
Deploy the generated JAR package to SCF as follows:
Log in to the Serverless console.
Click Create on the Function Service page.
On the Create page, select Create from scratch and configure the following items:
Function Type: Event-triggered function.
Runtime Environment: Java 8.
Submitting Method: Local ZIP file.
Execution: com.tencent.scfspringbootjava8.ScfHandler:: mainHandler
.
Function Code: Click to upload the ZIP package.
Keep the default values for other configuration items and click Complete to complete the function creation as shown below:
Was this page helpful?