Build a Java Hello World on Alibaba Cloud Serverless in 8 Simple Steps
This guide walks you through Alibaba Cloud's Serverless experience, showing how to create a Java Hello World function, configure the application, write and deploy the code, and test the endpoint, all with clear screenshots and a detailed walkthrough.
Yesterday I was attracted by a flashy screen from Alibaba Cloud and discovered a Serverless experience activity that many developers, especially those born after 1990, are still curious about.
Serverless has been relatively quiet in China, so many developers don’t really know what it is. If you share this curiosity, you can try a simple Hello World to get a direct feel for this futuristic product.
How to Participate
Official activity homepage: https://developer.aliyun.com/topic/yiqi/hol?utm_content=g_1000180354
Official guide: https://help.aliyun.com/document_detail/181573.html
If you are confident in your exploration ability, you can follow the official documentation directly; otherwise, you can use the detailed guide below to avoid pitfalls.
Step 1: Enter the product list and click “Create Application”.
Step 2: Choose the “Empty Application” template.
Serverless application server: “Function Compute FC”.
Programming language: Java.
Step 3: Fill in the application name and description as you like.
Step 4: Click the “Develop & Deploy” button on the newly created application card.
Step 5: Create helloworld.java.
package com.alibaba.serverless.helloworld;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.FunctionComputeLogger;
import com.aliyun.fc.runtime.FunctionInitializer;
import com.aliyun.fc.runtime.PojoRequestHandler;
import com.aliyuncs.utils.Base64Helper;
/**
* Corresponds to the function name in the deployment route:
* com.alibaba.serverless.helloworld.helloworld::handleRequest
*/
public class helloworld implements FunctionInitializer, PojoRequestHandler<ApiGwRequest, ApiGwResponse> {
/** Cloud function initialization callback */
@Override
public void initialize(Context context) {
FunctionComputeLogger logger = context.getLogger();
logger.debug(String.format("RequestID is %s %n", context.getRequestId()));
}
/** Web cloud function entry point for each invocation */
@Override
public ApiGwResponse handleRequest(ApiGwRequest request, Context context) {
FunctionComputeLogger logger = context.getLogger();
String requestPath = request.getPath();
String bodyContext = "";
if (requestPath.startsWith("")) {
bodyContext = "你好,世界!";
}
ApiGwResponse response = new ApiGwResponse();
response.getHeaders().put("responseHeader", "testValue");
response.getHeaders().put("Content-type", "text/html; charset=utf-8");
boolean isBase = false;
if (isBase) {
try {
bodyContext = Base64Helper.encode(bodyContext, "UTF-8");
} catch (UnsupportedEncodingException e) {
isBase = false;
}
}
response.setBody(bodyContext);
response.setBase64Encoded(isBase);
response.setStatusCode(200);
logger.info("Response返回:" + bodyContext);
return response;
}
}Step 6: Submit the code as shown in the screenshot.
Step 7: Deploy the application to the daily environment by clicking the first icon on the left toolbar, then “Add a Route”. The route should be set to com.alibaba.serverless.helloworld.helloworld::handleRequest, which maps to the handleRequest method in helloworld.java.
Note: The request method must be set to POST; the testing tool defaults to POST, which is why this configuration is necessary.
Step 8: Test the /helloworld endpoint using the built‑in testing tool. After entering the path and clicking test, you will see the response “你好,世界!”.
The testing tool does not allow you to choose the HTTP method, so it always sends a POST request. If you missed this setting, the test will fail.
How was your first Serverless journey? Click the link below to try this future‑oriented development approach yourself.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
