Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
    • Overview
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes
  • FAQ
  1. Developing in Java
  2. Programming model
  3. Spring Boot

Using SpringBootApplication annotation to set a handler in Java

Written by
Yandex Cloud
Updated at February 7, 2025

You can set a Java handler by loading a Spring Boot application with an entry point as a class with the SpringBootApplication annotation.

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 invoke the function at https://functions.yandexcloud.net/function-id/api/v1/list. Instead, you will have to provide path data in the request body (url parameter) or use API Gateway integration. We recommend the second option because API Gateway is the simplest you can use with a Spring Boot application and enables you to address application endpoints the usual way.

In case your application logic uses the HttpServletRequest and HttpServletResponse classes, please note that Cloud Functions does not support some methods of these classes. You can review the list of unsupported methods here.

Cloud Functions does not support Spring Boot Loader.

Example of a simple application with an endpointExample 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.

  1. Create a function.

  2. Create a ZIP archive with the following hierarchy:

    src
    |__main
        |__java
            |__util
                |__Application.java
                |__controller
                    |__TestController.java
    pom.xml
    
    • 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);
          }
      }
      
    • 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 file:

      <?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>
      
  3. Create a function version and specify:

    • Runtime environment: java17.
    • Method: ZIP archive.
    • File: Upload the archive you created earlier.
    • Timeout: 30.
    • Memory: 128 MB.
    • Entry point: util.Application.
  4. 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 the functions.functionInvoker role.
  5. Create a request to the endpoint:

    curl \
      --request GET \
      --header "Authorization: Bearer ${IAM_TOKEN}" \
      https://d5dm1lba80md********.i9******.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
}

Was the article helpful?

Previous
HttpServlet class
Next
Managing dependencies
© 2025 Direct Cursus Technology L.L.C.