Skip to content

Commit dc6e7f6

Browse files
authored
sample code for custom-serialization (#71) (#513)
1 parent 6538136 commit dc6e7f6

File tree

41 files changed

+1189
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1189
-5
lines changed

.github/workflows/samples.yml

+26-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ on:
77
push:
88
branches: [ main ]
99
paths:
10-
- 'samples/kinesis-firehose-event-handler/**'
10+
- 'samples/**'
1111
pull_request:
1212
branches: [ '*' ]
1313
paths:
14-
- 'samples/kinesis-firehose-event-handler/**'
14+
- 'samples/**'
1515
- '.github/workflows/samples.yml'
1616

1717
jobs:
18-
build:
19-
18+
build-kinesis-sample:
2019
runs-on: ubuntu-latest
21-
2220
steps:
2321
- uses: actions/checkout@v4
2422
- name: Set up JDK 1.8
@@ -37,3 +35,26 @@ jobs:
3735
# Install samples
3836
- name: Install Kinesis Firehose Sample with Maven
3937
run: mvn -B install --file samples/kinesis-firehose-event-handler/pom.xml
38+
39+
custom-serialization:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v4
43+
- uses: actions/setup-java@v4
44+
with:
45+
java-version: 21
46+
distribution: corretto
47+
# Build custom-serialization samples
48+
- name: install sam
49+
uses: aws-actions/setup-sam@v2
50+
- name: test fastJson
51+
run: cd samples/custom-serialization/fastJson && sam build && sam local invoke -e events/event.json | grep 200
52+
- name: test gson
53+
run: cd samples/custom-serialization/gson && sam build && sam local invoke -e events/event.json | grep 200
54+
- name: test jackson-jr
55+
run: cd samples/custom-serialization/jackson-jr && sam build && sam local invoke -e events/event.json | grep 200
56+
- name: test moshi
57+
run: cd samples/custom-serialization/moshi && sam build && sam local invoke -e events/event.json | grep 200
58+
- name: test request-stream-handler
59+
run: cd samples/custom-serialization/request-stream-handler && sam build && sam local invoke -e events/event.json | grep 200
60+
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
**/target/
2+
**/HelloWorld.iml
3+
**/samconfig.toml
4+
**/dependency-reduced-pom.xml
5+
**/.aws-sam
6+
**/.gradle
7+
**/bin
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The Lambda Java managed runtimes support custom serialization for JSON events.
2+
https://docs.aws.amazon.com/lambda/latest/dg/java-custom-serialization.html
3+
4+
## Sample projects
5+
In this repository you will find a number of sample projects from AWS to help you get started with the custom serialization feature.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>helloworld</groupId>
5+
<artifactId>HelloWorld</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<name>A sample Hello World created for SAM CLI.</name>
9+
<properties>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.amazonaws</groupId>
17+
<artifactId>aws-lambda-java-core</artifactId>
18+
<version>1.2.3</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
<version>3.14.0</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>com.alibaba.fastjson2</groupId>
28+
<artifactId>fastjson2</artifactId>
29+
<version>2.0.33</version>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-shade-plugin</artifactId>
38+
<version>3.2.4</version>
39+
<configuration>
40+
</configuration>
41+
<executions>
42+
<execution>
43+
<phase>package</phase>
44+
<goals>
45+
<goal>shade</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package com.example.vehicles.serialization;
7+
8+
import com.alibaba.fastjson2.JSON;
9+
import com.alibaba.fastjson2.JSONException;
10+
import com.amazonaws.services.lambda.runtime.CustomPojoSerializer;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
import java.lang.reflect.Type;
14+
15+
public class FastJsonSerializer implements CustomPojoSerializer {
16+
/**
17+
* ServiceLoader class requires that the single exposed provider type has a default constructor
18+
* to easily instantiate the service providers that it finds
19+
*/
20+
public FastJsonSerializer() {
21+
}
22+
23+
@Override
24+
public <T> T fromJson(InputStream input, Type type) {
25+
try {
26+
return JSON.parseObject(input, type);
27+
} catch (JSONException e) {
28+
throw (e);
29+
}
30+
}
31+
32+
@Override
33+
public <T> T fromJson(String input, Type type) {
34+
try {
35+
return JSON.parseObject(input, type);
36+
} catch (JSONException e) {
37+
throw (e);
38+
}
39+
}
40+
41+
@Override
42+
public <T> void toJson(T value, OutputStream output, Type type) {
43+
try {
44+
JSON.writeTo(output, value);
45+
} catch (JSONException e) {
46+
throw (e);
47+
}
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helloworld;
7+
8+
import com.amazonaws.services.lambda.runtime.Context;
9+
import com.amazonaws.services.lambda.runtime.RequestHandler;
10+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
11+
12+
/**
13+
* Handler for requests to Lambda function.
14+
*/
15+
public class App implements RequestHandler<Vehicle, APIGatewayProxyResponseEvent> {
16+
17+
public APIGatewayProxyResponseEvent handleRequest(Vehicle vehicle, Context context) {
18+
System.out.println("input: " + vehicle);
19+
20+
return new APIGatewayProxyResponseEvent().withStatusCode(200);
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package helloworld;
7+
8+
import com.alibaba.fastjson2.annotation.JSONField;
9+
10+
public class Vehicle {
11+
12+
@JSONField(name = "vehicle-type")
13+
private String vehicleType;
14+
15+
@JSONField(name = "vehicleID")
16+
private String vehicleId;
17+
18+
public Vehicle() {
19+
}
20+
21+
public Vehicle(String vehicleType, String vehicleId) {
22+
this.vehicleType = vehicleType;
23+
this.vehicleId = vehicleId;
24+
}
25+
26+
public String getVehicleType() {
27+
return vehicleType;
28+
}
29+
30+
public void setVehicleType(String vehicleType) {
31+
this.vehicleType = vehicleType;
32+
}
33+
34+
public String getVehicleId() {
35+
return vehicleId;
36+
}
37+
38+
public void setVehicleId(String vehicleId) {
39+
this.vehicleId = vehicleId;
40+
}
41+
42+
@Override
43+
public String toString() {
44+
return "Vehicle{" +
45+
"vehicleType='" + vehicleType + '\'' +
46+
", vehicleId='" + vehicleId + '\'' +
47+
'}';
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.example.vehicles.serialization.FastJsonSerializer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Build and test commands
2+
3+
```bash
4+
sam build
5+
sam local invoke -e events/event.json
6+
```
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"vehicle-type": "car",
3+
"vehicleID": 123
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: >
4+
fastJson
5+
6+
Sample SAM Template for fastJson
7+
8+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
9+
Globals:
10+
Function:
11+
Timeout: 20
12+
MemorySize: 512
13+
14+
Resources:
15+
HelloWorldFunction:
16+
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
17+
Properties:
18+
CodeUri: HelloWorldFunction
19+
Handler: helloworld.App::handleRequest
20+
Runtime: java21
21+
Architectures:
22+
- x86_64
23+
MemorySize: 512
24+
Events:
25+
HelloWorld:
26+
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
27+
Properties:
28+
Path: /hello
29+
Method: get
30+
31+
Outputs:
32+
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
33+
# Find out more about other implicit resources you can reference within SAM
34+
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
35+
HelloWorldApi:
36+
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
37+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
38+
HelloWorldFunction:
39+
Description: "Hello World Lambda Function ARN"
40+
Value: !GetAtt HelloWorldFunction.Arn
41+
HelloWorldFunctionIamRole:
42+
Description: "Implicit IAM Role created for Hello World function"
43+
Value: !GetAtt HelloWorldFunctionRole.Arn
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>helloworld</groupId>
5+
<artifactId>HelloWorld</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<name>A sample Hello World created for SAM CLI.</name>
9+
<properties>
10+
<maven.compiler.source>21</maven.compiler.source>
11+
<maven.compiler.target>21</maven.compiler.target>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.amazonaws</groupId>
17+
<artifactId>aws-lambda-java-core</artifactId>
18+
<version>1.2.3</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
<version>3.14.0</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.google.code.gson</groupId>
27+
<artifactId>gson</artifactId>
28+
<version>2.11.0</version>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-shade-plugin</artifactId>
37+
<version>3.2.4</version>
38+
<configuration>
39+
</configuration>
40+
<executions>
41+
<execution>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>shade</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>

0 commit comments

Comments
 (0)