tencent cloud

Serverless Cloud Function

Release Notes and Announcements
Release Notes
Announcements
User Guide
Product Introduction
Overview
Related Concepts
How It Works
Strengths
Scenarios
Related Products
Purchase Guide
Billing Overview
Billing Mode
Billable Items and Billing Modes
Function Computing Power Support
Free Tier
SCF Pricing
Billing Example
Payment Overdue
Getting Started
Creating Event Function in Console
User Guide
Quota Management
Managing Functions
Web Function Management
Log Management
Concurrence Management
Trigger Management
Function URL
A Custom Domain Name
Version Management
Alias Management
Permission Management
Running Instance Management
Plugin Management
Managing Monitors and Alarms
Network Configuration
Layer Management
Execution Configuration
Extended Storage Management
DNS Caching Configuration
Resource Managed Mode Management
Near-Offline Resource Hosting Model
Workflow
Triggers
Trigger Overview
Trigger Event Message Structure Summary
API Gateway Trigger
COS Trigger
CLS Trigger
Timer Trigger
CKafka Trigger
Apache Kafka Trigger
MQTT Trigger
Trigger Configuration Description
MPS Trigger
CLB Trigger Description
TencentCloud API Trigger
Development Guide
Basic Concepts
Testing a Function
Environment Variables
Dependency Installation
Using Container Image
Error Types and Retry Policies
Dead Letter Queue
Connecting SCF to Database
Automated Deployment
Cloud Function Status Code
Common Errors and Solutions
Developer Tools
Serverless Web IDE
Calling SDK Across Functions
Third-Party Tools
Code Development
Python
Node.js
Golang
PHP
Java
Custom Runtime
Deploying Image as Function
Web Framework Development
Deploying Framework on Command Line
Quickly Deploying Egg Framework
Quickly Deploying Express Framework
Quickly Deploying Flask Framework
Quickly Deploying Koa Framework
Quickly Deploying Laravel Framework
Quickly Deploying Nest.js Framework
Quickly Deploying Next.js Framework
Quickly Deploying Nuxt.js Framework
Quickly Deploying Django Framework
Use Cases
Overview
Solutions with Tencent Cloud Services
Business Development
TRTC Practices
COS Practices
CKafka Practice
CLS
CLB Practice
MPS
CDN
CDWPG
VOD
SMS
ES
Scheduled Task
Video Processing
Success Stories
Tencent Online Education
Online Video Industry
Tencent Online Education
Best Practice of Tencent IEG Going Global
API Documentation
History
Introduction
API Category
Making API Requests
Other APIs
Namespace APIs
Layer Management APIs
Async Event Management APIs
Trigger APIs
Function APIs
Function and Layer Status Description
Data Types
Error Codes
SDK Documentation
FAQs
General
Web Function
Billing FAQs
Network FAQs
Log FAQs
SCF utility class
Event Handling FAQs
API Gateway Trigger FAQs
Related Agreement
Service Level Agreement
Contact Us
Glossary

Common Examples

PDF
フォーカスモード
フォントサイズ
最終更新日: 2024-12-02 18:12:22

Scenario

With POJO type parameters, you can handle more complex data structures than simple event input parameters. This document uses an example to describe how to use POJO parameters in SCF and which input parameter formats are supported.

Prerequisites

You have logged in to the SCF console.

Directions

Event input parameters and POJOs

The event input parameters used in this document are as follows:
{
"person": {"firstName":"bob","lastName":"zou"},
"city": {"name":"shenzhen"}
}
With the input parameters above, the following content is outputted:
{
"greetings": "Hello bob zou.You are from shenzhen"
}
Based on the input parameters, the following four classes are built:
RequestClass: Used to accept the event as the class that accepts events.
PersonClass: Used to handle the person section in the event JSON.
CityClass: Used to handle the city section in the event JSON.
ResponseClass: Used to assemble the response content.

Prepare the code

Prepare the code by following the steps below for the four classes constructed based on the input parameters and entry function:

Step 1. Prepare the project directory

Create a project root directory, such as scf_example.

Step 2. Prepare the code directory

1. Create a folder src\\main\\java as the code directory in the project root directory.
2. According to the name of the package to be used, create a package folder in the code directory. For example, create example to form the directory structure scf_example\\src\\main\\java\\example.

Step 3. Prepare the code

Create files Pojo.java, RequestClass.java, PersonClass.java, CityClass.java, and ResponseClass.java in the example folder with the following contents respectively:
Pojo.java
RequestClass.java
PersonClass.java
CityClass.java
ResponseClass.java
package example;

public class Pojo{
public ResponseClass handle(RequestClass request){
String greetingString = String.format("Hello %s %s.You are from %s", request.person.firstName, request.person.lastName, request.city.name);
return new ResponseClass(greetingString);
}
}

package example;

public class RequestClass {
PersonClass person;
CityClass city;

public PersonClass getPerson() {
return person;
}

public void setPerson(PersonClass person) {
this.person = person;
}

public CityClass getCity() {
return city;
}

public void setCity(CityClass city) {
this.city = city;
}

public RequestClass(PersonClass person, CityClass city) {
this.person = person;
this.city = city;
}

public RequestClass() {
}

}

package example;

public class PersonClass {
String firstName;
String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public PersonClass(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public PersonClass() {
}

}

package example;

public class CityClass {
String name;


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public CityClass(String name) {
this.name = name;
}

public CityClass() {
}

}

package example;

public class ResponseClass {
String greetings;

public String getGreetings() {
return greetings;
}

public void setGreetings(String greetings) {
this.greetings = greetings;
}

public ResponseClass(String greetings) {
this.greetings = greetings;
}

public ResponseClass() {
}

}


Code compilation

Note
In the example, Maven is chosen to compile and package the code. You can choose another packaging method according to your own conditions.
1. Create the pom.xml function in the project root directory and enter the following content:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>examples</groupId>
<artifactId>java-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>java-example</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. Run the mvn package command on the command line and make sure that there is a successful compilation message. If the output result is as follows, the packaging is successful:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.800 s
[INFO] Finished at: 2017-08-25T15:42:41+08:00
[INFO] Final Memory: 18M/309M
[INFO] ------------------------------------------------------------------------
If the compilation fails, please modify the code as prompted.
3. The generated package after compilation is located at target\\java-example-1.0-SNAPSHOT.jar.

Function creation and testing

1. Create a function as instructed in Create a Function.
2. Upload the compiled package as a submission package. You can choose to upload the packaging by zipping it or uploading it to a COS bucket and then submitting it through COS bucket upload.
3. Set the execution method of the function to example.Pojo::handle.
4. Enter the input parameters that are expected to be processed in the Test event template on the Functions page:
{ "person": {"firstName":"bob","lastName":"zou"}, "city": {"name":"shenzhen"}}
After clicking Execute, you can see the returned result:
{ "greetings": "Hello bob zou.You are from shenzhen"}
You can also modify the values of the structures in the test input parameters, and you can see the modification effect after execution.

Sample

You can go to scf-demo-java to pull the sample code for testing.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック