How to run Spring Cloud Azure Functions with Java 17?

Souza Gustavo (BD/XDA3) 0 Pontos de reputação
2025-05-09T17:31:45.23+00:00

Spring Boot 3 only has support to Java 17, 21 and 24.

I see that there is a Spring Cloud Functions Azure Adapter, which should help doing that, however it is missing a step-by-step guide.

So here's my step-by-step attempt:

  1. Used spring initializr to set a project with spring boot 3.4.5 and Java 17, dependencies: lombok
  2. I executed mvn wrapper:wrapper in order to download mvn wrapper
  3. I added the following dependency to my pom.xml
       <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-function-adapter-azure</artifactId>
         <version>4.2.2</version>
       </dependency>
    
  4. I added the following plugin to my pom.xml
       
       <plugin>
           <groupId>com.microsoft.azure</groupId>
           <artifactId>azure-functions-maven-plugin</artifactId>
           <version>1.38.0</version>
           <configuration>
            <appName>scf-samples</appName>
            <resourceGroup>java-functions-group</resourceGroup>
            <region>westus</region>
            <appServicePlanName>java-functions-app-service-plan</appServicePlanName>
            <pricingTier>EP1</pricingTier>
            <hostJson>${project.basedir}/src/main/resources/host.json</hostJson>
       
            <runtime>
             <os>linux</os>
             <javaVersion>17</javaVersion>
            </runtime>
       
            <funcPort>7072</funcPort>
       
            <appSettings>
             <property>
              <name>FUNCTIONS_EXTENSION_VERSION</name>
              <value>~4</value>
             </property>
            </appSettings>
           </configuration>
           <executions>
            <execution>
             <id>package-functions</id>
             <goals>
              <goal>package</goal>
             </goals>
            </execution>
           </executions>
          </plugin>
         </plugins>
       </build>
    
  5. I added the following host.json file under main/resources directory
       {
         "version": "2.0",
         "extensionBundle": {
           "id": "Microsoft.Azure.Functions.ExtensionBundle",
           "version": "[4.*, 5.0.0)"
         }
       }
    
  6. Added bean definition in the main class
       import java.util.function.Function;
       import org.springframework.boot.SpringApplication;
       import org.springframework.boot.autoconfigure.SpringBootApplication;
       import org.springframework.context.annotation.Bean;
       
       @SpringBootApplication
       public class ServerlessApplication {
       
         public static void main(String[] args) {
          SpringApplication.run(ServerlessApplication.class, args);
         }
       
         @Bean
         public UpperCaseFunction uppercase() {
          return String::toUpperCase;
         }
       
         @Bean
         public Function<String, String> reverse() {
          return payload -> new StringBuilder(payload).reverse().toString();
         }
       
       }
    
  7. Created a new Spring Component
       import com.microsoft.azure.functions.ExecutionContext;
       import com.microsoft.azure.functions.HttpMethod;
       import com.microsoft.azure.functions.HttpRequestMessage;
       import com.microsoft.azure.functions.annotation.AuthorizationLevel;
       import com.microsoft.azure.functions.annotation.FunctionName;
       import com.microsoft.azure.functions.annotation.HttpTrigger;
       import java.util.Optional;
       import java.util.function.Function;
       import org.springframework.beans.factory.annotation.Autowired;
       import org.springframework.cloud.function.context.FunctionCatalog;
       import org.springframework.stereotype.Component;
       
       @Component
       public class AzureJavaExampleFunctionWithSpring {
           /**
            * Plain Spring bean (not Spring Cloud Functions!)
            */
           @Autowired
           private Function<String, String> uppercase;
       
           /**
            * The FunctionCatalog leverages the Spring Cloud Function framework.
            */
           @Autowired
           private FunctionCatalog functionCatalog;
       
           @FunctionName("bean")
           public String plainBeans(
               @HttpTrigger(name = "req", methods = { HttpMethod.GET,
                   HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
               ExecutionContext context) {
       
             // Use plain Spring Beans.
             return uppercase.apply(request.getBody().orElse("Hello World"));
           }
       
           @FunctionName("scf")
           public String springCloudFunction(
               @HttpTrigger(name = "req", methods = { HttpMethod.GET,
                   HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
               ExecutionContext context) {
       
             // Use SCF composition.
             Function composed = this.functionCatalog.lookup("reverse|uppercase");
       
             return (String) composed.apply(request.getBody().orElse("Hello World"));
           }
       }
       
    
  8. Compiled using maven wrapper
       ./mvnw clean install
       ./mvnw clean package
    
  9. Run function locally
       ./mvnw azure-functions:run
    
    I expected this to work as there were no build errors, but here's what I got: [2025-05-09T16:34:20.687Z] Worker process started and initialized. [2025-05-09T16:34:20.736Z] Worker failed to load function: 'scf' with functionId: 'bd71dc2c-1d4e-4d09-8ce0-3806fefe07a4'. [2025-05-09T16:34:20.737Z] Result: Failure [2025-05-09T16:34:20.736Z] Worker failed to load function: 'bean' with functionId: 'cdd49f11-c457-4d7d-a5bd-e503461b6b7e'. Then it says the reason is ClassNotFoundException for AzureJavaExampleFunctionWithSpring.
Azure
Azure

Uma plataforma de computação em nuvem e uma infraestrutura para criar, implantar e gerenciar aplicativos e serviços por meio de uma rede mundial de datacenters gerenciados pela Microsoft.


Sua resposta

As respostas podem ser marcadas como ‘Aceitas’ pelo autor da pergunta e ‘Recomendadas’ pelos moderadores, o que ajuda os usuários a saber a resposta que resolveu o problema do autor.