Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
    • Cloud Interconnect
    • Cloud Backup
    • Cloud Registry
    • Yandex AI Studio
    • Compute Cloud
    • Object Storage
    • Managed Service for Kubernetes®
    • Yandex BareMetal
    • Smart Web Security
    • Security Deck
    • Managed Service for PostgreSQL
    • Managed Service for ClickHouse®
    • Monium
    • Cloud CDN
    • Network Load Balancer
    • Virtual Private Cloud
    • Cloud DNS
    • Application Load Balancer
    • Yandex Cloud Video
    • Stackland
    • Yandex Cloud Router
    • Yandex Managed Service for Trino
    • Managed Service for MySQL®
    • Managed Service for Valkey™
    • Managed Service for Apache Spark™
    • Yandex StoreDoc
    • Managed Service for OpenSearch
    • Managed Service for Apache Kafka®
    • Data Transfer
    • Yandex MPP Analytics Engine for PostgreSQL
    • Yandex Managed Service for Apache Airflow®
    • Data Processing
    • Yandex MetaData Hub
    • Managed Service for YDB
    • Managed Service for Sharded PostgreSQL
    • Managed Service for YTsaurus
    • Yandex WebSQL
    • DataLens
    • Yandex Search API
    • SpeechSense
    • SpeechKit
    • DataSphere
    • Vision OCR
    • Translate
    • Yandex Identity Hub
    • Key Management Service
    • Certificate Manager
    • Yandex Lockbox
    • Audit Trails
    • SmartCaptcha
    • Cloud Desktop
    • SourceCraft Code Assistant
    • Container Registry
    • Managed Service for GitLab
    • Managed Service for Prometheus®
    • Cloud Functions
    • API Gateway
    • Yandex Cloud Postbox
    • Message Queue
    • Serverless Integrations
    • IoT Core
    • Data Streams
    • Serverless Containers
    • Cloud Notification Service
    • Yandex Query
    • Identity and Access Management
    • Yandex Cloud Console
    • Resource Manager
    • Yandex Cloud Billing
    • Yandex Cloud Quota Manager
    • Cloud Apps
  • System Status
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Cloud Functions
  • Comparing with other Yandex Cloud services
    • Overview
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Error handling
    • Using the SDK
  • Tools
  • Pricing policy
  • Access management
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes
  • FAQ

In this article:

  • Examples
  • HTTP request structure output
  • Parsing an HTTP request
  1. Developing in Java
  2. Programming model
  3. Function interface

Using the Function interface to set a handler in Java

Written by
Yandex Cloud
Updated at July 8, 2026
View in Markdown
  • Examples
    • HTTP request structure output
    • Parsing an HTTP request

You can set a handler function in Java by implementing the Function interface.

Warning

You must specify both values for the Function type parameters: the first one being the input argument type and the second one, the return value type.

Example of a valid handler:

import java.util.function.Function;

public class Handler implements Function<Integer, String> {
  @Override
  public String apply(Integer i) {
    return String.valueOf(i);
  }
}

Examples of invalid handlers:

import java.util.function.Function;
// Function has only one parameter type specified.
// Handler should not have any parameter types (for details, see handler requirements)
public class Handler<T> implements Function<T, Integer> {
  @Override
  public Integer apply(T i) {
    return 2;
  }
}
import java.util.function.Function;
// Function has neither parameter type specified.
public class Handler implements Function {
  @Override
  public Object apply(Object i) {
    return i;
  }
}

You can use any classes as input and return types.

Note

Fields of these classes can have any access modifiers. For non-public fields, defining the getter or setter method is optional.

ExamplesExamples

HTTP request structure outputHTTP request structure output

The following function receives a request with two fields (a string and a number) and returns a formatted string with the provided data.

Warning

To invoke the function, use the Yandex Cloud CLI or an HTTP request with the ?integration=raw parameter.

Request.java:

public class Request {
  public String message;
  public int number;
}

Handler.java:

import java.util.function.Function;

public class Handler implements Function<Request, String> {

  @Override
  public String apply(Request r) {
    // At this point, the r variable already stores the parsed request.
    return String.format("Message is %s, number is %d", r.message, r.number);
  }
}

Example of input data:

{
  "message": "Hello",
  "number": 24
}

Returned string:

Message is Hello, number is 24

Parsing an HTTP requestParsing an HTTP request

The function is invoked via an HTTP request with a username, logs the request method and body, and returns a greeting.

Warning

Do not use ?integration=raw to invoke this function. If you do, the function will not get any data about the original request’s methods, headers, or parameters.

Note

This example uses the org.json third-party library for JSON parsing.

Request.java:

public class Request {
  public String httpMethod;
  public String body;
}

Response.java:

public class Response {
  public int statusCode;
  public String body;

  public Response(int statusCode, String body) {
    this.statusCode = statusCode;
    this.body = body;
  }
}

Handler.java:

import java.util.function.Function;
import org.json.*;

public class Handler implements Function<Request, Response> {

  @Override
  public Response apply(Request r) {
    var method = r.httpMethod;
    var body = r.body;
    System.out.println(String.format("%s, %s", method, body));

    var jsonObject = new JSONObject(body);
    // Here, the name value is retrieved from the request body.
    // If you do not provide it, an error will be thrown.
    var name = jsonObject.getString("name");
    return new Response(200, String.format("Hello, %s", name));
  }
}

Example of input data (the POST method):

{
  "name": "Anonymous"
}

The log will contain the following:

POST, { "name": "Anonymous" }

Response returned:

Hello, Anonymous

Was the article helpful?

Previous
Overview
Next
YcFunction interface
© 2026 Direct Cursus Technology L.L.C.