Skip to content

Commit 1df6ab7

Browse files
committed
Update HandlerStream code to show deserialization of custom Record object
Code courtesy of @msailes , thanks!
1 parent f9fffc1 commit 1df6ab7

File tree

7 files changed

+67
-53
lines changed

7 files changed

+67
-53
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "interactive"
3+
}

sample-apps/java-basic/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ repositories {
99
dependencies {
1010
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
1111
implementation 'org.slf4j:slf4j-nop:2.0.6'
12+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
1213
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
1314
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
1415
}
@@ -26,8 +27,9 @@ task buildZip(type: Zip) {
2627
}
2728

2829
java {
29-
sourceCompatibility = JavaVersion.VERSION_11
30-
targetCompatibility = JavaVersion.VERSION_11
30+
toolchain {
31+
languageVersion = JavaLanguageVersion.of(21)
32+
}
3133
}
3234

3335
build.dependsOn buildZip

sample-apps/java-basic/pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
<name>java-basic-function</name>
99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11-
<maven.compiler.source>11</maven.compiler.source>
12-
<maven.compiler.target>11</maven.compiler.target>
11+
<java.version>21</java.version>
12+
<maven.compiler.source>${java.version}</maven.compiler.source>
13+
<maven.compiler.target>${java.version}</maven.compiler.target>
14+
<maven.compiler.release>${java.version}</maven.compiler.release>
1315
</properties>
1416
<dependencies>
1517
<dependency>
@@ -22,6 +24,11 @@
2224
<artifactId>slf4j-nop</artifactId>
2325
<version>2.0.6</version>
2426
</dependency>
27+
<dependency>
28+
<groupId>com.fasterxml.jackson.core</groupId>
29+
<artifactId>jackson-databind</artifactId>
30+
<version>2.17.0</version>
31+
</dependency>
2532
<dependency>
2633
<groupId>org.junit.jupiter</groupId>
2734
<artifactId>junit-jupiter-api</artifactId>
Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,39 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
14
package example;
25

36
import com.amazonaws.services.lambda.runtime.Context;
4-
import com.amazonaws.services.lambda.runtime.LambdaLogger;
57
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
8+
import com.fasterxml.jackson.annotation.JsonProperty;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
610

7-
import java.io.InputStream;
8-
import java.io.InputStreamReader;
9-
import java.io.BufferedReader;
10-
import java.io.BufferedWriter;
1111
import java.io.IOException;
12+
import java.io.InputStream;
1213
import java.io.OutputStream;
13-
import java.io.OutputStreamWriter;
14-
import java.io.PrintWriter;
15-
import java.nio.charset.Charset;
14+
import java.util.List;
1615

17-
// Handler value: example.HandlerStream
1816
public class HandlerStream implements RequestStreamHandler {
1917

20-
@Override
21-
/*
22-
* Takes an InputStream and an OutputStream. Reads from the InputStream,
23-
* and copies all characters to the OutputStream.
24-
*/
25-
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException
26-
{
27-
LambdaLogger logger = context.getLogger();
28-
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("US-ASCII")));
29-
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, Charset.forName("US-ASCII"))));
30-
int nextChar;
31-
try {
32-
while ((nextChar = reader.read()) != -1) {
33-
outputStream.write(nextChar);
34-
}
35-
} catch (IOException e) {
36-
e.printStackTrace();
37-
} finally {
38-
reader.close();
39-
String finalString = writer.toString();
40-
logger.log("Final string result: " + finalString);
41-
writer.close();
18+
private static final ObjectMapper objectMapper = new ObjectMapper();
19+
20+
@Override
21+
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
22+
Order order = objectMapper.readValue(input, Order.class);
23+
24+
processOrder(order);
25+
OrderAccepted orderAccepted = new OrderAccepted(order.orderId);
26+
27+
objectMapper.writeValue(output, orderAccepted);
4228
}
43-
}
29+
30+
private void processOrder(Order order) {
31+
// business logic
32+
}
33+
34+
public record Order(@JsonProperty("orderId") String orderId, @JsonProperty("items") List<Item> items) { }
35+
36+
public record Item(@JsonProperty("name") String name, @JsonProperty("quantity") Integer quantity) { }
37+
38+
public record OrderAccepted(@JsonProperty("orderId") String orderId) { }
4439
}

sample-apps/java-basic/src/test/java/example/InvokeTest.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package example;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
43
import static org.junit.jupiter.api.Assertions.assertEquals;
54
import static org.junit.jupiter.api.Assertions.assertNotNull;
65
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -14,6 +13,7 @@
1413
import java.io.ByteArrayInputStream;
1514
import java.io.ByteArrayOutputStream;
1615
import java.io.IOException;
16+
import java.io.OutputStream;
1717
import java.nio.charset.StandardCharsets;
1818
import java.util.HashMap;
1919
import java.util.List;
@@ -58,20 +58,27 @@ void testHandlerList() {
5858
}
5959

6060
@Test
61-
void testHandlerStream() throws IOException {
62-
logger.info("Invoke TEST - HandlerStream");
63-
String inputStr = "Hello world";
64-
byte[] inputBytes = inputStr.getBytes();
65-
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputBytes);
66-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
67-
Context context = new TestContext();
68-
HandlerStream handler = new HandlerStream();
69-
handler.handleRequest(inputStream, outputStream, context);
70-
byte[] outputBytes = outputStream.toByteArray();
71-
assertArrayEquals(inputBytes, outputBytes);
72-
String outputStr = new String(outputBytes, StandardCharsets.UTF_8);
73-
assertEquals(inputStr, outputStr);
74-
}
61+
public void testHandlerStream() throws IOException {
62+
HandlerStream orderProcessor = new HandlerStream();
63+
String input = """
64+
{
65+
"orderId": "123",
66+
"items": [
67+
{
68+
"name": "widgets",
69+
"quantity": 10
70+
}
71+
]
72+
}
73+
""";
74+
Context context = null;
75+
ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
76+
OutputStream outputStream = new ByteArrayOutputStream();
77+
78+
orderProcessor.handleRequest(inputStream, outputStream, context);
79+
80+
assertEquals("{\"orderId\":\"123\"}", outputStream.toString());
81+
}
7582

7683
@Test
7784
void testHandlerString() {

sample-apps/java-basic/template-mvn.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Resources:
77
Properties:
88
CodeUri: target/java-basic-1.0-SNAPSHOT.jar
99
Handler: example.Handler
10-
Runtime: java11
10+
Runtime: java21
1111
Description: Java function
1212
MemorySize: 2048
1313
Timeout: 10

sample-apps/java-basic/template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Resources:
77
Properties:
88
CodeUri: build/distributions/java-basic.zip
99
Handler: example.Handler
10-
Runtime: java11
10+
Runtime: java21
1111
Description: Java function
1212
MemorySize: 2048
1313
Timeout: 10

0 commit comments

Comments
 (0)