tencent cloud

Feedback

Implementing Todo Application with Spring Boot and SCF

Last updated: 2022-05-20 18:50:51

    Overview

    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).

    Prerequisites

    You have prepared the development environment and tools as instructed in Notes on Java.

    Directions

    Using HTTP-triggered function

    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.

    Creating function

    1. Log in to the SCF console.
    2. Click Create on the Function Service page.
    3. On the Create page, select Template and search for springboot and webfunc. In the search results, select SpringBootToDoApplication and click Next as shown below:
    4. Keep the default configuration and click Complete to complete the function creation.

    Testing function

    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:

    Sample code

    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 the scf_bootstrap file to ensure that the file has the execution permission.

    • Package the scf_bootstrap file and the generated JAR package into a ZIP package and deploy it to SCF as follows:
      1. Log in to the SCF console.
      2. Click Create on the Function Service page.
      3. On the Create page, select Create from scratch and configure the following items:
        Function Type: HTTP-triggered function.
        Runtime Environment: Java 8.
        Submitting Method: Local ZIP file.
        Function Code: Click to upload the ZIP package.
      4. Keep the default values for other configuration items and click Complete to complete the function creation as shown below:

    Using event-triggered function

    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.

    Creating function

    1. Log in to the SCF console.
    2. Click Create on the Function Service page.
    3. On the Create page, select Template and search for springboot. In the search results, select SpringBoot and click Next.
    4. Keep the default configuration and click Complete to complete the function creation.

    Creating trigger

    Note:

    If you have created an API Gateway trigger during function creation, simply check whether its configuration is the same as that described below.

    1. After creating a function, on the Trigger Management tab, click Create Trigger.
    2. Configure the trigger in the pop-up window as shown below, keep the default values for other configuration items, and click Submit.
    • Trigger Method: API Gateway trigger
    • Integration Response: Enabled
    1. After the creation, you need to adjust the API Gateway trigger parameters. Click API Name to enter the API Gateway console for subsequent operations as shown below:
    2. In the API Gateway console, find the API used by the function and click Edit as shown below:

      On the frontend configuration page, change the path to /todos, click Complete Now, and release the service as prompted, as shown below:

    Testing function

    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:

    Sample code

    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:

    • Add the 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.
      The project structure after the 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:

      1. Log in to the SCF console.

      2. Click Create on the Function Service page.

      3. 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.

      4. Keep the default values for other configuration items and click Complete to complete the function creation as shown below:

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support