Using SpringBootApplication annotation to set a handler in Java
You can set a Java handler by loading a Spring Boot
application with an entry point as a class with SpringBootApplication
When being executed, the Cloud Functions function has no data about the path that was used to invoke it. In other words, if there is an /api/v1/list
endpoint in your Spring Boot
application, you will not be able to use https://functions.yandexcloud.net/function-id/api/v1/list
to call your function. Instead, you will have to provide path data in the request body (url
parameter) or use API Gateway integration. We recommend utilizing the second method because the API Gateway
is easiest to use with a Spring Boot
application and enables you to access application endpoints as usual.
In case your application logic uses the HttpServletRequest
Cloud Functions does not support Spring Boot Loader.
Example of a simple application with an endpoint
The following application has a single endpoint: GET: /get/{name}
. In response to a GET
request at /get
with the specified path parameter, the function returns Hello, $name
, where $name
is the provided path parameter. In our example, we use a public function. If your function is private, specify a service account with the functions.functionInvoker
role in the API gateway specification.
-
Create a function.
-
Create a ZIP archive with the following hierarchy:
src |__main |__java |__util |__Application.java |__controller |__TestController.java pom.xml
-
The
Application.java
file:package util; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
-
The
TestController.java
file:package util.controller; import org.springframework.web.bind.annotation.*; @RestController public class TestController { @GetMapping("/get/{name}") public String get(@PathVariable String name) { return String.format("Hello, %s!", name); } }
-
pom.xml
:<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.7</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>util</groupId> <artifactId>util</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>11</java.version> <spring.version>5.2.9.RELEASE</spring.version> <spring.boot.version>2.3.4.RELEASE</spring.boot.version> <start-class>util.Application</start-class> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
-
Create a function version and specify:
- Runtime environment:
java17
. - Method:
ZIP archive
. - File: Upload the previously created archive.
- Timeout, sec:
30
. - Memory:
128 MB
. - Entry point:
util.Application
.
- Runtime environment:
-
Create an API Gateway API gateway and add the following specification:
openapi: 3.0.0 info: title: Test API version: 1.0.0 paths: /get/{name}: get: x-yc-apigateway-integration: type: cloud-functions function_id: <function_ID> service_account_id: <service_account_ID> operationId: get parameters: - description: my param explode: false in: path name: name required: true schema: type: string style: simple
Where:
function_id
: Function ID.service_account_id
: Service account with thefunctions.functionInvoker
role.
-
Create a request to the endpoint:
curl -X GET -H "Authorization: Bearer ${IAM_TOKEN}" \ https://<gateway_ID>.apigw.yandexcloud.net/get/Anonymous
Result:
Hello, Anonymous
Sample direct request where API Gateway is not used to invoke the function:
{
"httpMethod": "GET",
"url": "/get/Anonymous",
"requestContext": {},
"body": "",
"isBase64Encoded": false
}