Skip to content

Update the Spring Cloud Function sample to Spring Boot 3.2.0 #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions samples/spring-cloud-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To develop functions using Java, you must have the following installed:

> [!IMPORTANT]
> 1. You must set the JAVA_HOME environment variable to the install location of the JDK to complete this quickstart.
> 2. Make sure your core tools version is at least 4.0.5030
> 2. Make sure your core tools version is at least 4.0.5455

## What we're going to build

Expand Down Expand Up @@ -46,19 +46,17 @@ Change those properties directly near the top of the *pom.xml* file, as shown in
```xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>

<azure.functions.maven.plugin.version>1.22.0</azure.functions.maven.plugin.version>
<azure.functions.java.library.version>3.0.0</azure.functions.java.library.version>
<!-- Spring Boot start class. WARNING: correct class must be set -->
<start-class>com.example.DemoApplication</start-class>

<!-- customize those two properties. The functionAppName should be unique across Azure -->
<!-- customize those properties. WARNING:: the functionAppName should be unique across Azure -->
<functionResourceGroup>my-spring-function-resource-group</functionResourceGroup>
<functionAppServicePlanName>my-spring-function-service-plan</functionAppServicePlanName>
<functionAppName>my-spring-function</functionAppName>

<functionAppRegion>westeurope</functionAppRegion>
<start-class>example.Application</start-class>
<functionAppRegion>eastus</functionAppRegion>
<functionPricingTier>Y1</functionPricingTier>
</properties>

```
Expand All @@ -74,7 +72,7 @@ Create a *src/main/azure* folder and add the following Azure Functions configura
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
"version": "[4.*, 5.2.0)"
},
"functionTimeout": "00:10:00"
}
Expand Down Expand Up @@ -162,33 +160,32 @@ This capability gives you two main benefits over a standard Azure Function:
- It doesn't rely on the Azure Functions APIs, so you can easily port it to other systems. For example, you can reuse it in a normal Spring Boot application.
- You can use all the `@Enable` annotations from Spring Boot to add new features.

In the *src/main/java/example* folder, create the following file, which is a normal Spring Boot application:
In the *src/main/java/com/example* folder, create the following file, which is a normal Spring Boot application:

*DemoApplication.java*:

```java
package example;
package com.example;

import example.uppercase.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public class DemoApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(Config.class, args);
SpringApplication.run(DemoApplication.class, args);
}
}
```

Now create the following file *src/main/java/example/hello* folder, which contains a Spring Boot component that represents the Function we want to run:
Now create the following file *src/main/java/com/example/hello* folder, which contains a Spring Boot component that represents the Function we want to run:

*Hello.java*:

```java
package example.hello;
package com.example.hello;

import example.hello.model.*;
import com.example.model.*;
import org.springframework.stereotype.Component;
import java.util.function.Function;

Expand Down Expand Up @@ -217,13 +214,13 @@ In the *src/main/java/com/example/hello* folder, create the following Azure Func
*HelloHandler.java*:

```java
package example.hello;
package com.example.hello;

import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import example.hello.model.*;
import com.example.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -237,14 +234,12 @@ public class HelloHandler {

@FunctionName("hello")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request, ExecutionContext context) {
User user = request.getBody()
.filter(u -> u.getName() != null)
.orElseGet(() -> new User(request.getQueryParameters().getOrDefault("name", "world")));
context.getLogger().info("Greeting user name: " + user.getName());
return request
.createResponseBuilder(HttpStatus.OK)
return request.createResponseBuilder(HttpStatus.OK)
.body(hello.apply(user))
.header("Content-Type", "application/json")
.build();
Expand All @@ -267,10 +262,11 @@ Create a *src/test/java/com/example* folder and add the following JUnit tests:
*HelloTest.java*:

```java
package example.hello;
package com.example;

import example.hello.model.Greeting;
import example.hello.model.User;
import com.example.hello.Hello;
import com.example.model.Greeting;
import com.example.model.User;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
35 changes: 11 additions & 24 deletions samples/spring-cloud-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,37 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>3.2.0</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.example.azure.di</groupId>
<groupId>com.example</groupId>
<artifactId>azure-httptrigger-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>azure-httptrigger-demo</name>

<description>Demo Spring Boot, Azure Function - HttpTrigger (DI adapter)</description>

<properties>
<java.version>17</java.version>
<spring-boot-thin-layout.version>1.0.28.RELEASE</spring-boot-thin-layout.version>
<java.version>11</java.version>

<!-- Spring Boot start class! WARING: correct class must be set! -->
<start-class>example.Application</start-class>
<!-- Spring Boot start class. WARNING: correct class must be set -->
<start-class>com.example.DemoApplication</start-class>

<!-- AZURE FUNCTION CONFIG -->
<azure.functions.maven.plugin.version>1.22.0</azure.functions.maven.plugin.version>
<!-- customize those properties. WARNING: the functionAppName should be unique across Azure -->
<azure.functions.maven.plugin.version>1.29.0</azure.functions.maven.plugin.version>
<functionAppName>spring-cloud-function-samples</functionAppName>
<functionAppRegion>westus</functionAppRegion>
<functionResourceGroup>sample-resource-group</functionResourceGroup>
<functionAppServicePlanName>sample-service-plan</functionAppServicePlanName>
<functionPricingTier>EP1</functionPricingTier>
<functionPricingTier>Y1</functionPricingTier>
<functionAppRegion>eastus</functionAppRegion>
</properties>
<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
<version>4.0.0</version>
<version>4.0.5</version>
</dependency>

<dependency>
Expand All @@ -48,7 +47,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -99,18 +98,6 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>${spring-boot-thin-layout.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package example;

import example.uppercase.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Config.class, args);
}
}
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package example.hello;
package com.example.hello;

import example.hello.model.*;
import com.example.model.*;
import org.springframework.stereotype.Component;
import java.util.function.Function;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package example.hello;
package com.example.hello;

import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import example.hello.model.*;
import com.example.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -18,14 +18,12 @@ public class HelloHandler {

@FunctionName("hello")
public HttpResponseMessage execute(
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
ExecutionContext context) {
@HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request, ExecutionContext context) {
User user = request.getBody()
.filter(u -> u.getName() != null)
.orElseGet(() -> new User(request.getQueryParameters().getOrDefault("name", "world")));
context.getLogger().info("Greeting user name: " + user.getName());
return request
.createResponseBuilder(HttpStatus.OK)
return request.createResponseBuilder(HttpStatus.OK)
.body(hello.apply(user))
.header("Content-Type", "application/json")
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package example.hello.model;
package com.example.model;

public class Greeting {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package example.hello.model;
package com.example.model;

public class User {

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 3 additions & 2 deletions samples/spring-cloud-example/src/main/resources/host.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
"version": "[4.*, 5.2.0)"
},
"functionTimeout": "00:10:00"
}
Loading