Using SpringBootApplication annotation to set a handler in Java
You can set a handler in Java by running a Spring Boot application with an entry point as a class annotated with SpringBootApplication
While running, a function in Cloud Functions has no data about the path used to invoke it. In other words, if your Spring Boot application has an /api/v1/list endpoint, you will not be able to invoke a function at https://functions.yandexcloud.net/function-id/api/v1/list. Instead, you will have to provide path data in the request body (the url parameter) or use API Gateway integration. We recommend the second option, since API Gateway works best with Spring Boot applications and enables you to access application endpoints in the usual way.
In case your application’s 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 path parameter specified, the function will return 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 ZIP archive with the following structure:
src |__main |__java |__util |__Application.java |__controller |__TestController.java pom.xml-
Application.java: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); } } -
TestController.java: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. - Code source:
ZIP archive. - File: Upload the archive you created earlier.
- Timeout:
30. - Memory:
128 MB. - Entry point:
util.Application.
- Runtime environment:
-
Create an API Gateway and add the following specification to it:
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: simpleWhere:
function_id: Function ID.service_account_id: Service account with thefunctions.functionInvokerrole.
-
Create a request to the endpoint:
curl \ --request GET \ --header "Authorization: Bearer ${IAM_TOKEN}" \ https://d5dm1lba80md********.i9******.apigw.yandexcloud.net/get/AnonymousResult:
Hello, Anonymous
Example of a direct request that does not use API Gateway to invoke the function:
{
"httpMethod": "GET",
"url": "/get/Anonymous",
"requestContext": {},
"body": "",
"isBase64Encoded": false
}