Skip to content

Commit f895a62

Browse files
committed
feat: migrate REST endpoints to Azure Functions HTTP triggers
- Replace UserController with UserHandler leveraging Azure Functions annotations - Enable deployment using local.settings.json and updated host.json location - Update pom.xml for Azure plugin and function adapter configuration - Remove unused web starter dependency and revise artifact for Azure support - Improve code modularity and compatibility with serverless platform
1 parent f5a3174 commit f895a62

6 files changed

Lines changed: 73 additions & 32 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@ build/
3333
.vscode/
3434

3535
### Azure Functions ###
36-
local.settings.json
3736
obj/
3837
bin/
File renamed without changes.

local.settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5+
"FUNCTIONS_WORKER_RUNTIME": "java",
6+
"FUNCTIONS_EXTENSION_VERSION": "~4"
7+
}
8+
}

pom.xml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@
2323
<functionAppName>springcloud-azurefunction</functionAppName>
2424
</properties>
2525
<dependencies>
26-
<dependency>
27-
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter-web</artifactId>
29-
</dependency>
3026
<dependency>
3127
<groupId>org.springframework.boot</groupId>
3228
<artifactId>spring-boot-starter-data-jpa</artifactId>
3329
</dependency>
3430
<dependency>
3531
<groupId>org.springframework.cloud</groupId>
36-
<artifactId>spring-cloud-function-adapter-azure-web</artifactId>
32+
<artifactId>spring-cloud-function-adapter-azure</artifactId>
3733
</dependency>
3834

3935
<dependency>
@@ -79,7 +75,8 @@
7975
<version>${azure.functions.maven.plugin.version}</version>
8076
<configuration>
8177
<appName>${functionAppName}</appName>
82-
<hostJson>${project.basedir}/src/main/resources/host.json</hostJson>
78+
<hostJson>${project.basedir}/host.json</hostJson>
79+
<localSettingsJson>${project.basedir}/local.settings.json</localSettingsJson>
8380
<runtime>
8481
<os>linux</os>
8582
<javaVersion>21</javaVersion>

src/main/java/io/jaylee/springcloud/controller/UserController.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.jaylee.springcloud.handler;
2+
3+
import com.microsoft.azure.functions.*;
4+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
5+
import com.microsoft.azure.functions.annotation.BindingName;
6+
import com.microsoft.azure.functions.annotation.FunctionName;
7+
import com.microsoft.azure.functions.annotation.HttpTrigger;
8+
import io.jaylee.springcloud.model.UserDTO;
9+
import io.jaylee.springcloud.service.UserService;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.Optional;
13+
14+
@Component
15+
public class UserHandler {
16+
17+
private final UserService userService;
18+
19+
public UserHandler(UserService userService) {
20+
this.userService = userService;
21+
}
22+
23+
@FunctionName("getUser")
24+
public HttpResponseMessage getUser(
25+
@HttpTrigger(name = "request",
26+
methods = {HttpMethod.GET},
27+
route = "user/{userId}",
28+
authLevel = AuthorizationLevel.ANONYMOUS)
29+
HttpRequestMessage<Optional<String>> request,
30+
@BindingName("userId") String userId,
31+
ExecutionContext context) {
32+
context.getLogger().info("Getting user: " + userId);
33+
UserDTO user = userService.getUser(userId);
34+
return request.createResponseBuilder(HttpStatus.OK)
35+
.body(user)
36+
.header("Content-Type", "application/json")
37+
.build();
38+
}
39+
40+
@FunctionName("createUser")
41+
public HttpResponseMessage createUser(
42+
@HttpTrigger(name = "request",
43+
methods = {HttpMethod.POST},
44+
route = "user",
45+
authLevel = AuthorizationLevel.ANONYMOUS)
46+
HttpRequestMessage<Optional<UserDTO>> request,
47+
ExecutionContext context) {
48+
context.getLogger().info("Creating user");
49+
UserDTO userDTO = request.getBody()
50+
.orElse(null);
51+
if (userDTO == null) {
52+
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
53+
.body("Request body is required")
54+
.build();
55+
}
56+
String userId = userService.saveUser(userDTO);
57+
return request.createResponseBuilder(HttpStatus.OK)
58+
.body(userId)
59+
.header("Content-Type", "application/json")
60+
.build();
61+
}
62+
}

0 commit comments

Comments
 (0)