eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

eBook – Guide Spring Cloud – NPI (cat=Cloud/Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

1. Overview

In this tutorial, we’ll explore Azure Java Functions. Azure Function is a serverless compute service that can execute code in response to events originating from Azure services or custom applications. We can write event-driven code in programming languages like Python, Powershell, Javascript, C#, and Typescript.

This feature allows automation engineers to develop apps that handle the events originating from the various Azure services. Azure Function provides a serverless hosting environment for running the code without worrying about infrastructure management. Hence, it facilitates quick deployment, auto-scaling, and easy maintenance of these automation apps.

2. High-Level Use Cases

Let’s see a few examples where we can use Azure Functions:

 

Azure Function

Azure Function is an excellent serverless service that helps handle events or data originating from different Azure services or custom applications.  Additionally, we can configure the function to run at specific schedules to poll the data from various sources and perform further processing. Even, custom applications can send messages to Function apps over HTTP protocol.

Functions can read data from the event context, transform or enrich them, and then send them to target systems. The event could be a new file uploaded to the Blob Storage container, a new message in the Queue Storage, or a topic in the Kafka streaming service. There could be more such scenarios related to other services.

Moreover, Azure’s Event Grid service can centrally manage events using a pub-sub architecture. Services can publish events to Event Grid, and subscriber applications can consume them. Function apps can consume and process events from Event Grid.

3. Key Concepts

Before writing code, we must know a few concepts about the Azure Functions programming model like bindings and triggers.

When we deploy code in Azure Functions, the code must provide the mandatory information on how it will be triggered. Azure uses this information to invoke the functions running in it. This is called a trigger. The Azure Java Function library provides the framework to specify the trigger declaratively with the help of annotations.

Similarly, the functions need the data relevant to the triggering source for further processing. This information can be provided with the help of input bindings. Also, there can be scenarios where the function must send some data to a target system. Output bindings can help achieve this. Unlike triggers, bindings are optional.

Let’s suppose that whenever a file is uploaded to Blob Storage, we have to insert its contents into a Cosmos DB database. Trigger would help us define the file upload event in the Blob Storage. Additionally, we can retrieve the file contents from the trigger. Furthermore, using output binding we can provide the information related to the target Cosmos DB where we’ll insert the data. In a way, it also helps return data from a function.

These concepts will be clearer in the upcoming sections.

4. Prerequisites

To experience the Azure Function in action, we’ll need an active subscription to Azure to create cloud resources.

IDEs like Eclipse, Visual Studio Code, and IntelliJ offer extensions or plugins to help develop, debug, test, and deploy Function apps in Azure using Maven-based tools. They help create scaffolds with all the necessary components of the framework to speed up development. However, we’ll use IntelliJ with the Azure Toolkit plugin for this tutorial.

Although the toolkit plugin helps create the Java project with the necessary Maven dependency, it’s important to take a look at it:

<dependency>
    <groupId>com.microsoft.azure.functions</groupId>
    <artifactId>azure-functions-java-library</artifactId>
    <version>3.1.0</version>
</dependency>

Azure functions Maven plugin helps package and deploy the Azure function:

<plugin>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-functions-maven-plugin</artifactId>
    <version>1.24.0</version>
</plugin>

This plugin helps specify the deployment configurations such as Azure Function’s name, resource group, runtime environment, settings, etc.:

<configuration>
    <appName>${functionAppName}</appName>
    <resourceGroup>java-functions-group</resourceGroup>
    <appServicePlanName>java-functions-app-service-plan</appServicePlanName>
    <region>westus</region>
    <runtime>
        <os>windows</os>
        <javaVersion>17</javaVersion>
    </runtime>
    <appSettings>
        <property>
            <name>FUNCTIONS_EXTENSION_VERSION</name>
            <value>~4</value>
        </property>
        <property>
            <name>AZURE_STORAGE</name>
            <value>DefaultEndpointsProtocol=https;AccountName=functiondemosta;AccountKey=guymcrXX..XX;EndpointSuffix=core.windows.net</value>
        </property>
    </appSettings>
</configuration>

The azure-functions plugin packages the whole application in a predefined standard folder structure:

deployment

The function.json file created for each trigger endpoint defines the entry point and the bindings of the application:

{
  "scriptFile" : "../azure-functions-1.0.0-SNAPSHOT.jar",
  "entryPoint" : "com.baeldung.functions.BlobTriggerJava.run",
  "bindings" : [ {
    "type" : "blobTrigger",
    "direction" : "in",
    "name" : "content",
    "path" : "feeds/{name}.csv",
    "dataType" : "binary",
    "connection" : "AZURE_STORAGE"
  }, {
    "type" : "cosmosDB",
    "direction" : "out",
    "name" : "output",
    "databaseName" : "organization",
    "containerName" : "employee",
    "connection" : "COSMOS_DB"
  } ]
}

Finally, once we execute the Maven goals clean, compile, package, and azure-functions:deploy in IntelliJ, it deploys the application into Azure Function:

 

azure deploy

If the Function service doesn’t exist in the Azure subscription, the plugin creates or updates it with the latest configurations. Interestingly, along with the Function, the azure-functions Maven plugin creates a bunch of other supporting essential cloud resources as well:

java-fn-group

5. Key Components of Azure Function SDK

While developing the Azure functions in Java, we mostly deal with annotations to declare triggers and bindings. A few important triggers applied to functions are @HttpTrigger, @BlobTrigger, @CosmosDBTrigger, @EventGridTrigger, @TimerTrigger, etc.

The input binding annotations such as @BlobInput, @CosmosDBInput, @TableInput, etc. complement the triggers to help functions access the data from the event sources. Understandably, we can apply them to the input arguments of the function.

On the other hand, the output binding annotations such as @BlobOutput, @CosmosDBOutput, @TableOutput, etc. are applied on function arguments of type OutputBinding<T>. Moreover, they help update the data received from the source into the target systems like Blob Storages, Cosmos DB, Storage Tables, etc.

Additionally, we may use certain interfaces such as ExecutionContext, HttpRequestMessage<T>HttpResponseMessage.Builder, HttpResponseMessage<T>,  OutputBinding<T> , etc. ExecutionContext is one of the arguments to the function and it helps access the runtime environments such as logger, invocation id, etc. The other interfaces help receive the HTTP request payload in the parameters, and form and then return the HTTP response messages.

Let’s understand these components with the help of sample code in the next section.

6. Java Implementation

We’ll implement a few use cases using Azure Function and learn about this framework. Similarly, we can later apply the same concepts to the annotations not discussed in this article.

6.1. Move HTTP Request Payload to Storage Table

Let’s create a function that receives employee data in the HTTP request payload and then updates an employee Azure Storage Table:

@FunctionName("addEmployee")
@StorageAccount("AZURE_STORAGE")
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.POST }, route = "employee/{partitionKey}/{rowKey}",
  authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<Employee>> empRequest,
  @BindingName("partitionKey") String partitionKey,
  @BindingName("rowKey") String rowKey,
  @TableOutput(name = "data", tableName = "employee") OutputBinding<Employee> employeeOutputBinding,
  final ExecutionContext context) {
    context.getLogger().info("Received a http request: " + empRequest.getBody().toString());

    Employee employee = new Employee(empRequest.getBody().get().getName(),
      empRequest.getBody().get().getDepartment(),
      empRequest.getBody().get().getSex(),
      partitionKey, rowKey);
    employeeOutputBinding.setValue(employee);

    return empRequest.createResponseBuilder(HttpStatus.OK)
      .body("Employee Inserted")
      .build();
}

Client programs can trigger this function by submitting a POST HTTP request at the API endpoint https://{Azure Function URL}/api/employee/{partitionKey}/{rowKey}?code={function access key}. The request body should constitute employee data in JSON format. The clients can send the partition and the row key of the table record in the partitionKey and rowKey URI path variables. The @BindingName annotation helps bind the input path variables in the partitionKey and rowKey method variables.

In the method’s body, we create the Employee object from the HTTP request body and then set it to the output binding employeeOutputBinding argument. We provide the storage table information in the tableName attribute of the @TableOutput annotation applied to the method argument employeeOutputBinding. There’s no need to explicitly call any API to insert data into the employee table.

The @StorageAccount annotation specifies the connection string of the employee table’s Storage Account in the value AZURE_STORAGE variable. We can store it as a runtime environment variable in the application’s settings:

 

Azure appsettings

Let’s now invoke the endpoint for inserting a record in the employee table:

 

postman

Additionally, for troubleshooting purposes, we can check the function logs in the Azure portal:

 

appinsight

Finally, the JSON data gets inserted into the employee table in the Azure Storage Account:

 

azure storage table

6.2. Move Blob Data to Cosmos DB

In the previous use case, we explicitly invoked the HTTP endpoint. However, this time let’s consider an example to trigger a function automatically when a file is uploaded to a Blob Storage. Afterward, the functions reads the data and inserts it into a Cosmos DB database:

@FunctionName("BlobTriggerJava")
@StorageAccount("AZURE_STORAGE")
public void run(
  @BlobTrigger(name = "content", path = "feeds/{name}.csv", dataType = "binary") byte[] content,
  @BindingName("name") String fileName,
  @CosmosDBOutput(name = "output",
    databaseName = "organization",
    connection = "COSMOS_DB",
    containerName = "employee") OutputBinding<List<Employee>> employeeOutputBinding,
  final ExecutionContext context) {
    context.getLogger().info("Java Blob trigger function processed a blob. Name: " + fileName + "\n  Size: " + content.length + " Bytes");
    employeeOutputBinding.setValue(getEmployeeList(content));
    context.getLogger().info("Processing finished");
}

The @BlobTrigger annotation’s path attribute helps specify the file in the Blob Storage. The function populates the argument employeeOutputBinding of type OutputBinding<List<Employee>> with the file’s content. We define the target Cosmos DB details in the @CosmosDBOutput annotation. The connection attribute’s value COSMOS_DB is an environment variable in the function’s application settings in Azure. It contains the target DB’s connection string.

To demonstrate, we’ll upload an employee.csv file to the Blob container under a Storage Account databasesas from the IntelliJ IDE:

 

azure explorer intellij

Finally, the function gets invoked and inserts the data into the Cosmos DB employee container:

 

cosmosdb

7. Conclusion

In this article, we learned the Azure Function’s programming model in Java. The framework is well-designed and simple to understand. However, it’s important to understand the basic troubleshooting steps when the function fails to trigger or cannot update the target system. For this, we must learn the concept of Azure Functions and the complementary services such as Storage Account, Application Insight, etc.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
eBook – eBook Guide Spring Cloud – NPI (cat=Cloud/Spring Cloud)