This document details how to develop functions by using Java from NHN Cloud's Cloud Functions service.
| Item | Value |
|---|---|
| Supported version | 17, 21 |
| File name | HelloWorld.java |
| Entry Point | example.HelloWorld |
A basic form of function.
package example;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
public class HelloWorld {
public ResponseEntity<?> call(RequestEntity<?> req) {
return ResponseEntity.ok("Hello World!");
}
}
In a Java function, you can access HTTP request information through RequestEntity.
package example;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import java.net.URI;
import java.util.Map;
public class HelloWorld {
public ResponseEntity<?> call(RequestEntity<Map<String, Object>> req) {
// HTTP request information
HttpMethod method = req.getMethod();
URI url = req.getUrl();
HttpHeaders headers = req.getHeaders();
Map<String, Object> body = req.getBody();
// Response data configuration
String responseBody = String.format(
"Method: %s\nURL: %s\nHeaders: %s\nBody: %s",
method, url, headers, body
);
return ResponseEntity.ok(responseBody);
}
}
You can download the Java template provided by Cloud Functions to develop a local environment.
Template download link: java.zip
The structure of the downloaded template file is as follows:
java.zip
├── pom.xml
└── src
└── main
└── java
└── example
└── HelloWorld.java
# Unzip
unzip java.zip -d my-java-function
# Move to task directory
cd my-java-function
Modify src/main/java/example/HelloWorld.java file with the logic you want.
// HelloWorld.java - Simple modification example
package example;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class HelloWorld {
public ResponseEntity<?> call(RequestEntity<Map<String, String>> req) {
try {
// Import name from query parameter
String name = Optional.ofNullable(req.getUrl().getQuery())
.map(q -> q.split("="))
.filter(p -> p.length > 1 && p[0].equals("name"))
.map(p -> p[1])
.orElse("World");
// Import message from body if POST request
String customMessage = "";
if (req.getMethod() == org.springframework.http.HttpMethod.POST && req.hasBody()) {
customMessage = req.getBody().getOrDefault("message", "");
}
// Response data configuration
Map<String, String> response = new HashMap<>();
response.put("greeting", "Hello, " + name + "!");
response.put("message", customMessage);
response.put("method", req.getMethod().toString());
response.put("timestamp", ZonedDateTime.now().format(DateTimeFormatter.ISO_INSTANT));
return ResponseEntity.ok(response);
} catch (Exception e) {
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("error", "Internal Server Error");
errorResponse.put("details", e.getMessage());
return ResponseEntity.status(500).body(errorResponse);
}
}
}
Compress the modified source code into ZIP file. You must compress it so that pom.xml and src are included at the top level.
# Windows (PowerShell)
Compress-Archive -Path .\pom.xml, .\src -DestinationPath my-function.zip
# Windows (when using 7-Zip)
7z a my-function.zip pom.xml src
# macOS/Linux
zip -r my-function.zip pom.xml src
# Include all files (exclude unnecessary files)
zip -r my-function.zip . -x "*.git*" "target/*" "*.log"
my-function.zip file you created.pom.xml.pom.xml and src directory must be located directly in the root of the ZIP file.target directory, .git directory, etc.package example;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class GetHandler {
public ResponseEntity<?> call(RequestEntity<Void> req) {
if (req.getMethod() != org.springframework.http.HttpMethod.GET) {
return ResponseEntity.status(405).body("Method Not Allowed");
}
// Import query parameter
String name = Optional.ofNullable(req.getUrl().getQuery())
.map(q -> URLDecoder.decode(q, StandardCharsets.UTF_8))
.map(q -> q.split("=")[1])
.orElse("World");
Map<String, String> response = new HashMap<>();
response.put("message", "Hello, " + name + "!");
response.put("timestamp", Instant.now().toString());
response.put("method", "GET");
return ResponseEntity.ok(response);
}
}
package example;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class PostHandler {
public ResponseEntity<?> call(RequestEntity<Map<String, String>> req) {
if (req.getMethod() != org.springframework.http.HttpMethod.POST) {
return ResponseEntity.status(405).body("Method Not Allowed");
}
if (!req.hasBody()) {
return ResponseEntity.badRequest().body("Request body is missing");
}
Map<String, String> requestBody = req.getBody();
String name = requestBody.get("name");
if (name == null || name.isEmpty()) {
return ResponseEntity.badRequest().body("Missing required field: name");
}
// Processing logic
Map<String, String> response = new HashMap<>();
response.put("id", UUID.randomUUID().toString().substring(0, 8));
response.put("name", name);
response.put("email", requestBody.getOrDefault("email", "not provided"));
response.put("processed_at", Instant.now().toString());
return ResponseEntity.status(201).body(response);
}
}
pom.xml)Use the pom.xml file to manage dependencies. When you add required libraries to the dependencies section, Cloud Functions automatically downloads the dependencies and includes them in the build when you upload your function.
<dependencies>
<!-- Basic Spring Boot dependencies (include Jackson) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.3.2</version>
<scope>provided</scope>
</dependency>
<!-- Example of adding Apache Commons Lang3 library -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
package example;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import java.util.HashMap;
import java.util.Map;
public class StringUtilsHandler {
public ResponseEntity<?> call(RequestEntity<Map<String, String>> req) {
if (!req.hasBody()) {
return ResponseEntity.badRequest().body("Request body is missing");
}
String originalText = req.getBody().get("text");
if (StringUtils.isEmpty(originalText)) {
return ResponseEntity.badRequest().body("Body must contain a 'text' field.");
}
// String manipulation using Apache Commons Lang3 StringUtils
Map<String, Object> response = new HashMap<>();
response.put("original", originalText);
response.put("reversed", StringUtils.reverse(originalText));
response.put("isAlphanumeric", StringUtils.isAlphanumeric(originalText));
response.put("wordCount", StringUtils.split(originalText, ' ').length);
return ResponseEntity.ok(response);
}
}
Use packagename.classname as an Entry Point.
exampleHelloWorldexample.HelloWorldpublic ResponseEntity<?> call(RequestEntity<?> req).src/main/java directory structure.